Skip to content

Commit 6a8fb86

Browse files
committed
Add more comparison tests
1 parent 519e13c commit 6a8fb86

4 files changed

Lines changed: 117 additions & 1 deletion

File tree

JavaScript/3-instantiation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function makeClosure(hello, size, flag) {
88

99
function closureInstance() {
1010
return makeClosure('world', 100500, true);
11-
};
11+
}
1212

1313
function defineArray() {
1414
return ['world', 100500, true];

JavaScript/e-copy-dataset.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
3+
const benchmark = require('./2-benchmark.js');
4+
5+
// Prepare data
6+
7+
const arr = [];
8+
for (let i = 0; i < 100; i++) {
9+
arr[i] = { name: 'item' + ((i - 50) * i) };
10+
}
11+
12+
// Different implementations
13+
14+
function testSlice() {
15+
return arr.slice();
16+
}
17+
18+
function testSlice0() {
19+
return arr.slice(0);
20+
}
21+
22+
function testConcat() {
23+
return [].concat(arr);
24+
}
25+
26+
function testFor() {
27+
const result = [];
28+
let len = arr.length;
29+
let i;
30+
for (i = 0; i < len; i++) {
31+
result.push(arr[i]);
32+
}
33+
return result;
34+
}
35+
36+
function testForNew() {
37+
let len = arr.length;
38+
const result = new Array(len);
39+
let i;
40+
for (i = 0; i < len; i++) {
41+
result[i] = arr[i];
42+
}
43+
return result;
44+
}
45+
46+
function testForNewLet() {
47+
let len = arr.length;
48+
const result = new Array(len);
49+
for (let i = 0; i < len; i++) {
50+
result[i] = arr[i];
51+
}
52+
return result;
53+
}
54+
55+
// Run tests
56+
57+
benchmark.do(500000, [
58+
testSlice,
59+
testSlice0,
60+
testConcat,
61+
testFor,
62+
testForNew,
63+
testForNewLet
64+
]);

JavaScript/f-for-of-each.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
const benchmark = require('./2-benchmark.js');
4+
5+
const data1 = [
6+
-100, -50, -30, -20, -10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
7+
];
8+
9+
const data2 = [
10+
'Descartes', 'Pascal', 'Leibniz', 'Euler', 'Bernoulli', 'Kepler'
11+
];
12+
13+
function testEachNumber() {
14+
const a = Array(data1.length);
15+
data1.forEach((item, i) => {
16+
a[i] = data1[i];
17+
});
18+
return a;
19+
}
20+
21+
function testForOfNumber() {
22+
const a = Array(data1.length);
23+
let i = 0;
24+
for (let n of data1) {
25+
a[i++] = n;
26+
}
27+
return a;
28+
}
29+
30+
function testEachString() {
31+
const a = Array(data2.length);
32+
data2.forEach((item, i) => {
33+
a[i] = data2[i];
34+
});
35+
return a;
36+
}
37+
38+
function testForOfString() {
39+
const a = Array(data2.length);
40+
let i = 0;
41+
for (let s of data2) {
42+
a[i++] = s;
43+
}
44+
return a;
45+
}
46+
47+
benchmark.do(5000000, [
48+
testEachNumber,
49+
testForOfNumber,
50+
testEachString,
51+
testForOfString,
52+
]);

0 commit comments

Comments
 (0)