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

fix: use native performance object in Node 18 #38

Merged
merged 3 commits into from
Jul 4, 2022
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
2 changes: 1 addition & 1 deletion .github/workflows/test_node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [ '12', '14', '16' ]
node: [ '12', '14', '16', '18' ]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
Expand Down
12 changes: 11 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,19 @@ let clear
if (
perf &&
perf.mark &&
perf.measure &&
perf.getEntriesByName &&
perf.getEntriesByType &&
perf.clearMeasures
perf.clearMarks &&
perf.clearMeasures &&
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems prudent to check for every API we might use, and not rely on measure existing if mark exists, and clearMarks existing if clearMeasures exists.

// In Node, we want to detect that this perf/correctness fix [1] is available, which
// landed in Node 16.15.0, 17.6.0, and 18.0.0. However, it's not observable, and
// we don't want to rely on fragile version checks.
// So we can rely on this observable change [2] to add clearResourceTimings, which
// landed a bit later (18.2.0), but is close enough for our purposes.
// [1]: https://github.com/nodejs/node/pull/42032
// [2]: https://github.com/nodejs/node/pull/42725
(process.browser || perf.clearResourceTimings)
) {
mark = name => {
throwIfEmpty(name)
Expand Down
10 changes: 5 additions & 5 deletions src/now.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import perf from './performance'

let nowForNode
let nowPolyfillForNode

if (!process.browser) {
// implementation borrowed from:
Expand All @@ -11,9 +11,9 @@ if (!process.browser) {
return hr[0] * 1e9 + hr[1]
}
const loadTime = getNanoSeconds()
nowForNode = () => ((getNanoSeconds() - loadTime) / 1e6)
nowPolyfillForNode = () => ((getNanoSeconds() - loadTime) / 1e6)
}

export default process.browser
? perf && perf.now ? () => perf.now() : () => Date.now()
: nowForNode
export default perf && perf.now
? () => perf.now()
: process.browser ? () => Date.now() : nowPolyfillForNode
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could probably get a microsecond perf boost from doing const now = perf.now.bind(perf), but meh. I don't want to rely on bind, or on now() working without the proper this, etc.

5 changes: 1 addition & 4 deletions src/performance.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
/* global performance */

// TODO: Node's built-in performance API has poor performance for getEntriesByName()
// so currently we avoid it: https://github.com/nolanlawson/marky/issues/29
export default process.browser && typeof performance !== 'undefined' && performance
export default typeof performance !== 'undefined' && performance