Skip to content

Commit 406e623

Browse files
targosMylesBorins
authored andcommitted
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>
1 parent a175186 commit 406e623

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'use strict';
2+
3+
constcommon=require('../common.js');
4+
5+
constbench=common.createBenchmark(main,{
6+
method: ['property','string','variable','symbol'],
7+
millions: [1000]
8+
});
9+
10+
functionrunProperty(n){
11+
constobject={};
12+
vari=0;
13+
bench.start();
14+
for(;i<n;i++){
15+
object.p1=21;
16+
object.p2=21;
17+
object.p1+=object.p2;
18+
}
19+
bench.end(n/1e6);
20+
}
21+
22+
functionrunString(n){
23+
constobject={};
24+
vari=0;
25+
bench.start();
26+
for(;i<n;i++){
27+
object['p1']=21;
28+
object['p2']=21;
29+
object['p1']+=object['p2'];
30+
}
31+
bench.end(n/1e6);
32+
}
33+
34+
functionrunVariable(n){
35+
constobject={};
36+
constvar1='p1';
37+
constvar2='p2';
38+
vari=0;
39+
bench.start();
40+
for(;i<n;i++){
41+
object[var1]=21;
42+
object[var2]=21;
43+
object[var1]+=object[var2];
44+
}
45+
bench.end(n/1e6);
46+
}
47+
48+
functionrunSymbol(n){
49+
constobject={};
50+
constsymbol1=Symbol('p1');
51+
constsymbol2=Symbol('p2');
52+
vari=0;
53+
bench.start();
54+
for(;i<n;i++){
55+
object[symbol1]=21;
56+
object[symbol2]=21;
57+
object[symbol1]+=object[symbol2];
58+
}
59+
bench.end(n/1e6);
60+
}
61+
62+
functionmain(conf){
63+
constn=+conf.millions*1e6;
64+
65+
switch(conf.method){
66+
case'property':
67+
runProperty(n);
68+
break;
69+
case'string':
70+
runString(n);
71+
break;
72+
case'variable':
73+
runVariable(n);
74+
break;
75+
case'symbol':
76+
runSymbol(n);
77+
break;
78+
default:
79+
thrownewError('Unexpected method');
80+
}
81+
}

0 commit comments

Comments
 (0)