Skip to content

Commit 2b28956

Browse files
committed
Added a few JavaScript core performance tests
1 parent b83ed50 commit 2b28956

7 files changed

Lines changed: 157 additions & 4 deletions

File tree

JavaScript/2-benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = benchmark;
66
benchmark.do = (count, retry, tests) => {
77

88
for (let k = 0; k < retry; k++) {
9-
//tests.sort(function() {
9+
//tests.sort(() => {
1010
// return Math.random() - 0.5;
1111
//});
1212
tests.map(test);

JavaScript/3-instantiation.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function defineObject() {
2929
}
3030

3131
function mixinObject() {
32-
var obj = {};
32+
const obj = {};
3333
obj.hello = 'world';
3434
obj.size = 100500;
3535
obj.flag = true;
@@ -45,15 +45,15 @@ function newInstance() {
4545
}
4646

4747
function newObject() {
48-
var obj = new Object();
48+
const obj = new Object();
4949
obj.hello = 'world';
5050
obj.size = 100500;
5151
obj.flag = true;
5252
return obj;
5353
}
5454

5555
function objectCreate() {
56-
var obj = Object.create(objectCreate.prototype);
56+
const obj = Object.create(objectCreate.prototype);
5757
obj.hello = 'world';
5858
obj.size = 100500;
5959
obj.flag = true;

JavaScript/5-range.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
const benchmark = require('./2-benchmark.js');
4+
5+
benchmark.do(100000, 3, [
6+
function testRangePush() {
7+
rangePush(1, 1000);
8+
},
9+
function testRangeNew() {
10+
rangeNew(1, 1000);
11+
}
12+
]);
13+
14+
function rangePush(min, max) {
15+
const arr = [];
16+
let i;
17+
for (i = min; i <= max; i++) arr.push(i);
18+
return arr;
19+
}
20+
21+
function rangeNew(from, to) {
22+
if (to < from) return [];
23+
let len = to - from + 1;
24+
const range = new Array(len);
25+
let i;
26+
for (i = from; i <= to; i++) {
27+
range[i - from] = i;
28+
}
29+
return range;
30+
}
31+
32+
function rangeEx(range) {
33+
let from, to, toType, count, res;
34+
from = range[0];
35+
to = range[1];
36+
toType = typeof(to);
37+
if (toType === 'undefined') {
38+
to = range[2];
39+
res = api.common.range(from, to);
40+
} else if (toType !== 'number') {
41+
count = to[0];
42+
if (count < 0) {
43+
cpus = api.os.cpus().length;
44+
count = cpus + count;
45+
}
46+
range = api.common.range(from, from + count - 1);
47+
}
48+
return res;
49+
}

JavaScript/6-functions.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const benchmark = require('./2-benchmark.js');
4+
5+
let fnLambda = () => {};
6+
7+
let fnExpression = function() {
8+
};
9+
10+
function fnDeclaration() {
11+
}
12+
13+
benchmark.do(10000000, 5, [
14+
fnDeclaration,
15+
fnExpression,
16+
fnLambda,
17+
]);

JavaScript/7-includes.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const benchmark = require('./2-benchmark.js');
4+
5+
const data = ['abc', 'defg', 'hijklmn', 'opqrst', 'u', 'v', 'xyz'];
6+
7+
function testIndexOf() {
8+
return [
9+
data.indexOf('opqrst') !== -1,
10+
data.indexOf('qwerty') !== -1,
11+
data.indexOf('v') !== -1
12+
];
13+
}
14+
15+
function testIncludes() {
16+
return [
17+
data.includes('opqrst'),
18+
data.includes('qwerty'),
19+
data.includes('v')
20+
];
21+
}
22+
23+
benchmark.do(10000000, 5, [
24+
testIncludes,
25+
testIndexOf,
26+
]);

JavaScript/8-for-let.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const benchmark = require('./2-benchmark.js');
4+
5+
const data = ['abc', 'defg', 'hijklmn', 'opqrst', 'u', 'v', 'xyz'];
6+
7+
8+
function testLetFor() {
9+
const a = Array(1000);
10+
let i;
11+
for (i = 0; i < 1000; i++) {
12+
a[i] = i;
13+
}
14+
}
15+
16+
function testForLet() {
17+
const a = Array(1000);
18+
for (let i = 0; i < 1000; i++) {
19+
a[i] = i;
20+
}
21+
}
22+
23+
benchmark.do(1000000, 5, [
24+
testLetFor,
25+
testForLet,
26+
]);

JavaScript/9-for-in-let.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const benchmark = require('./2-benchmark.js');
4+
5+
const data = {
6+
a: 'abc',
7+
bcd: 'defg',
8+
efgh: 'hijklmn',
9+
ijk: 'opqrst',
10+
lmnopqrs: 'u',
11+
tvuwx: 'v',
12+
yz: 'xyz'
13+
};
14+
15+
function testLetForIn() {
16+
const a = Array(7);
17+
let i = 0;
18+
let key;
19+
for (key in data) {
20+
a[i++] = key;
21+
}
22+
}
23+
24+
function testForInLet() {
25+
const a = Array(7);
26+
let i = 0;
27+
for (let key in data) {
28+
a[i++] = key;
29+
}
30+
}
31+
32+
benchmark.do(10000000, 5, [
33+
testLetForIn,
34+
testForInLet,
35+
]);

0 commit comments

Comments
 (0)