-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: add benchmark for object properties
Adds a benchmark to compare the speed of property setting/getting in four cases: - Dot notation: `obj.prop = value` - Bracket notation with string: `obj['prop'] = value` - Bracket notation with string variable: `obj[prop] = value` - Bracket notation with Symbol variable: `obj[sym] = value` PR-URL: #10949 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
- Loading branch information
1 parent
3f6a2db
commit 850f85d
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,81 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
method: ['property', 'string', 'variable', 'symbol'], | ||
millions: [1000] | ||
}); | ||
|
||
function runProperty(n) { | ||
const object = {}; | ||
var i = 0; | ||
bench.start(); | ||
for (; i < n; i++) { | ||
object.p1 = 21; | ||
object.p2 = 21; | ||
object.p1 += object.p2; | ||
} | ||
bench.end(n / 1e6); | ||
} | ||
|
||
function runString(n) { | ||
const object = {}; | ||
var i = 0; | ||
bench.start(); | ||
for (; i < n; i++) { | ||
object['p1'] = 21; | ||
object['p2'] = 21; | ||
object['p1'] += object['p2']; | ||
} | ||
bench.end(n / 1e6); | ||
} | ||
|
||
function runVariable(n) { | ||
const object = {}; | ||
const var1 = 'p1'; | ||
const var2 = 'p2'; | ||
var i = 0; | ||
bench.start(); | ||
for (; i < n; i++) { | ||
object[var1] = 21; | ||
object[var2] = 21; | ||
object[var1] += object[var2]; | ||
} | ||
bench.end(n / 1e6); | ||
} | ||
|
||
function runSymbol(n) { | ||
const object = {}; | ||
const symbol1 = Symbol('p1'); | ||
const symbol2 = Symbol('p2'); | ||
var i = 0; | ||
bench.start(); | ||
for (; i < n; i++) { | ||
object[symbol1] = 21; | ||
object[symbol2] = 21; | ||
object[symbol1] += object[symbol2]; | ||
} | ||
bench.end(n / 1e6); | ||
} | ||
|
||
function main(conf) { | ||
const n = +conf.millions * 1e6; | ||
|
||
switch (conf.method) { | ||
case 'property': | ||
runProperty(n); | ||
break; | ||
case 'string': | ||
runString(n); | ||
break; | ||
case 'variable': | ||
runVariable(n); | ||
break; | ||
case 'symbol': | ||
runSymbol(n); | ||
break; | ||
default: | ||
throw new Error('Unexpected method'); | ||
} | ||
} |