Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf: Use computed offset passed in DateTime constructor #1576

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion benchmarks/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function runDateTimeSuite() {
const dt = DateTime.now();

suite
.add("DateTime.local", () => {
.add("DateTime.now", () => {
DateTime.now();
})
.add("DateTime.fromObject with locale", () => {
Expand All @@ -18,6 +18,9 @@ function runDateTimeSuite() {
.add("DateTime.local with numbers", () => {
DateTime.local(2017, 5, 15);
})
.add("DateTime.local with numbers and zone", () => {
DateTime.local(2017, 5, 15, 11, 7, 35, { zone: "America/New_York" });
})
.add("DateTime.fromISO", () => {
DateTime.fromISO("1982-05-25T09:10:11.445Z");
})
Expand Down
4 changes: 3 additions & 1 deletion src/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,9 @@ export default class DateTime {
if (unchanged) {
[c, o] = [config.old.c, config.old.o];
} else {
const ot = zone.offset(this.ts);
// If an offset has been passed and we have not been called from
// clone(), we can trust it and avoid the offset calculation.
const ot = isNumber(config.o) && !config.old ? config.o : zone.offset(this.ts);
c = tsToObj(this.ts, ot);
invalid = Number.isNaN(c.year) ? new Invalid("invalid input") : null;
c = invalid ? null : c;
Expand Down