Repository navigation
benchmark: add microbenchmarks for ES Map #7581
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Next
Next commit
benchmark: add microbenchmarks for ES Map
- Loading branch information
commit 8b75639a4d48e517856d23f4bd0873cd50f9a619
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 |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common.js'); | ||
| const assert = require('assert'); | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| method: ['object', 'nullProtoObject', 'fakeMap', 'map'], | ||
| millions: [10] | ||
| }); | ||
|
|
||
| function runObject(n) { | ||
| const m = {}; | ||
| var i = 0; | ||
| bench.start(); | ||
| for (; i < n; i++) { | ||
| m['i' + n] = n; | ||
| m['s' + n] = String(n); | ||
| assert.equal(m['i' + n], m['s' + n]); | ||
| m['i' + n] = undefined; | ||
| m['s' + n] = undefined; | ||
| } | ||
| bench.end(n / 1e6); | ||
| } | ||
|
|
||
| function runNullProtoObject(n) { | ||
| const m = Object.create(null); | ||
| var i = 0; | ||
| bench.start(); | ||
| for (; i < n; i++) { | ||
| m['i' + n] = n; | ||
| m['s' + n] = String(n); | ||
| assert.equal(m['i' + n], m['s' + n]); | ||
| m['i' + n] = undefined; | ||
| m['s' + n] = undefined; | ||
| } | ||
| bench.end(n / 1e6); | ||
| } | ||
|
|
||
| function fakeMap() { | ||
| const m = {}; | ||
| return { | ||
| get(key) { return m['$' + key]; }, | ||
| set(key, val) { m['$' + key] = val; }, | ||
| get size() { return Object.keys(m).length; }, | ||
| has(key) { return Object.prototype.hasOwnProperty.call(m, '$' + key); } | ||
| }; | ||
| } | ||
|
|
||
| function runFakeMap(n) { | ||
| const m = fakeMap(); | ||
| var i = 0; | ||
| bench.start(); | ||
| for (; i < n; i++) { | ||
| m.set('i' + n, n); | ||
| m.set('s' + n, String(n)); | ||
| assert.equal(m.get('i' + n), m.get('s' + n)); | ||
| m.set('i' + n, undefined); | ||
| m.set('s' + n, undefined); | ||
| } | ||
| bench.end(n / 1e6); | ||
| } | ||
|
|
||
| function runMap(n) { | ||
| const m = new Map(); | ||
| var i = 0; | ||
| bench.start(); | ||
| for (; i < n; i++) { | ||
| m.set('i' + n, n); | ||
| m.set('s' + n, String(n)); | ||
| assert.equal(m.get('i' + n), m.get('s' + n)); | ||
| m.set('i' + n, undefined); | ||
| m.set('s' + n, undefined); | ||
| } | ||
| bench.end(n / 1e6); | ||
| } | ||
|
|
||
| function main(conf) { | ||
| const n = +conf.millions * 1e6; | ||
|
|
||
| switch (conf.method) { | ||
| case 'object': | ||
| runObject(n); | ||
| break; | ||
| case 'nullProtoObject': | ||
| runNullProtoObject(n); | ||
| break; | ||
| case 'fakeMap': | ||
| runFakeMap(n); | ||
| break; | ||
| case 'map': | ||
| runMap(n); | ||
| break; | ||
| default: | ||
| throw new Error('Unexpected method'); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we trying to set the same two properties (
'i10000000'and's10000000') 10 million times? If so, maybe use keys like'a', 'b'. Or should the keys be'i' + iand's' + i?