-
Notifications
You must be signed in to change notification settings - Fork 258
perf(NODE-4194): improve objectId constructor performance #498
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
42cba25
wip
nbbeeken eabb86d
perf(NODE-4194): improve objectId constructor performance
nbbeeken 7b8b916
test: move benchmarks
nbbeeken d0f8055
test: remove oid mocha test
nbbeeken 3f21cee
lint!
nbbeeken c519f9c
comment
nbbeeken 81e71b3
fix: focus improvement on objectId
nbbeeken b6680ae
fix: lint
nbbeeken 036d1ba
increase iterations, print system info
nbbeeken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,5 @@ coverage/ | |
*.d.ts | ||
*.tgz | ||
docs/public | ||
|
||
*.0x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
import { performance } from 'perf_hooks'; | ||
import { readFile } from 'fs/promises'; | ||
|
||
const readJSONFile = async path => | ||
JSON.parse(await readFile(new URL(path, import.meta.url), { encoding: 'utf8' })); | ||
|
||
const ITERATIONS = 10_000; | ||
|
||
function average(array) { | ||
let sum = 0; | ||
for (const value of array) sum += value; | ||
return sum / array.length; | ||
} | ||
|
||
function testPerformance(fn, iterations = ITERATIONS) { | ||
let measurements = []; | ||
for (let i = 0; i < iterations; i++) { | ||
const start = performance.now(); | ||
fn(i); | ||
const end = performance.now(); | ||
measurements.push(end - start); | ||
} | ||
return average(measurements).toFixed(8); | ||
} | ||
|
||
async function main() { | ||
const [currentBSON, currentReleaseBSON, legacyBSONLib] = await Promise.all([ | ||
(async () => ({ | ||
lib: await import('../../lib/bson.js'), | ||
version: 'current local' | ||
}))(), | ||
(async () => ({ | ||
lib: await import('../../node_modules/bson_latest/lib/bson.js'), | ||
version: (await readJSONFile('../../node_modules/bson_latest/package.json')).version | ||
}))(), | ||
(async () => { | ||
const legacyBSON = (await import('../../node_modules/bson_legacy/index.js')).default; | ||
return { | ||
lib: { ...legacyBSON, ...legacyBSON.prototype }, | ||
version: (await readJSONFile('../../node_modules/bson_legacy/package.json')).version | ||
}; | ||
})() | ||
]).catch(error => { | ||
console.error(error); | ||
console.error( | ||
`Please run:\n${[ | ||
'npm run build', | ||
'npm install --no-save bson_legacy@npm:bson@1 bson_latest@npm:bson@latest' | ||
].join('\n')}` | ||
); | ||
process.exit(1); | ||
}); | ||
|
||
const documents = Array.from({ length: ITERATIONS }, () => | ||
currentReleaseBSON.lib.serialize({ | ||
_id: new currentReleaseBSON.lib.ObjectId(), | ||
field1: 'value1' | ||
}) | ||
); | ||
|
||
console.log(`\nIterations: ${ITERATIONS}`); | ||
|
||
for (const bson of [currentBSON, currentReleaseBSON, legacyBSONLib]) { | ||
console.log(`\nBSON@${bson.version}`); | ||
console.log( | ||
`deserialize({ oid, string }, { validation: { utf8: false } }) takes ${testPerformance(i => | ||
bson.lib.deserialize(documents[i], { validation: { utf8: false } }) | ||
)}ms on average` | ||
); | ||
|
||
const oidBuffer = Buffer.from('00'.repeat(12), 'hex'); | ||
console.log( | ||
`new Oid(buf) take ${testPerformance(() => new bson.lib.ObjectId(oidBuffer))}ms on average` | ||
); | ||
} | ||
|
||
console.log(); | ||
} | ||
|
||
main() | ||
.then(() => null) | ||
.catch(error => console.error(error)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/* globals window */ | ||
'use strict'; | ||
|
||
exports.assertArrayEqual = function (array1, array2) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.