Skip to content

Commit

Permalink
fix(@formatjs/ecma402-abstract): swap out some impls with native Date…
Browse files Browse the repository at this point in the history
…, 2x speed, #2813
  • Loading branch information
longlho committed Apr 12, 2021
1 parent 180c9db commit 463420c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 66 deletions.
40 changes: 0 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
"hoist-non-react-statics": "^3.3.2",
"http-server": "^0.12.3",
"husky": "^6.0.0",
"intl-messageformat-parser": "^6.4.4",
"jasmine-expect": "^5.0.0",
"jest": "26",
"json-stable-stringify": "^1.0.1",
Expand Down
16 changes: 3 additions & 13 deletions packages/ecma402-abstract/262.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,33 +181,23 @@ export function WeekDay(t: number) {
* @param y
*/
export function DayFromYear(y: number) {
return (
365 * (y - 1970) +
Math.floor((y - 1969) / 4) -
Math.floor((y - 1901) / 100) +
Math.floor((y - 1601) / 400)
)
return Date.UTC(y, 0) / MS_PER_DAY
}

/**
* https://tc39.es/ecma262/#sec-year-number
* @param y
*/
export function TimeFromYear(y: number) {
return MS_PER_DAY * DayFromYear(y)
return Date.UTC(y, 0)
}

/**
* https://tc39.es/ecma262/#sec-year-number
* @param t
*/
export function YearFromTime(t: number) {
const min = Math.ceil(t / MS_PER_DAY / 366)
let y = min
while (TimeFromYear(y) <= t) {
y++
}
return y - 1
return new Date(t).getUTCFullYear()
}

export function DaysInYear(y: number) {
Expand Down
24 changes: 13 additions & 11 deletions packages/icu-messageformat-parser/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test", "nodejs_binary", "pkg_npm")
load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test", "pkg_npm")
load("//tools:index.bzl", "bundle_karma_tests", "check_format", "generate_src_file", "ts_compile")
load("//tools:jest.bzl", "jest_test")

Expand Down Expand Up @@ -54,16 +54,18 @@ jest_test(
] + SRC_DEPS,
)

nodejs_binary(
name = "benchmark",
data = [
":dist",
"@npm//benchmark",
"@npm//intl-messageformat-parser",
"benchmark.js",
] + SRC_DEPS,
entry_point = "benchmark.js",
)
# Disable this for now until we figure out how to resolve @formatjs/ecma402-abstract
# from intl-messageformat-parser properly (without overwriting local dep version instead)
# nodejs_binary(
# name = "benchmark",
# data = [
# ":dist",
# "@npm//benchmark",
# "@npm//intl-messageformat-parser",
# "benchmark.js",
# ] + SRC_DEPS,
# entry_point = "benchmark.js",
# )

generated_file_test(
name = "tsconfig_json",
Expand Down
2 changes: 1 addition & 1 deletion packages/intl-durationformat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"url": "https://github.com/formatjs/formatjs/issues"
},
"dependencies": {
"@formatjs/ecma402-abstract": "1.5.4",
"@formatjs/ecma402-abstract": "1.6.4",
"tslib": "^2.1.0"
}
}

0 comments on commit 463420c

Please sign in to comment.