Skip to content

Commit 1c1c2be

Browse files
committed
JavaScript for-in tests
1 parent 40ac462 commit 1c1c2be

2 files changed

Lines changed: 74 additions & 54 deletions

File tree

JavaScript/9-for-in-let.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

JavaScript/9-for-in.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 testForKeys() {
16+
const a = Array(7);
17+
let i, key;
18+
const keys = Object.keys(data);
19+
const len = keys.length;
20+
for (i = 0; i < len; i++) {
21+
key = keys[i];
22+
a[i] = data[key];
23+
}
24+
}
25+
26+
27+
function testForIn() {
28+
const a = Array(7);
29+
let i = 0;
30+
for (let key in data) {
31+
a[i++] = data[key];
32+
}
33+
}
34+
35+
function testForInLet() {
36+
const a = Array(7);
37+
let i = 0;
38+
let key;
39+
for (key in data) {
40+
a[i++] = data[key];
41+
}
42+
}
43+
44+
function testForOf() {
45+
const a = Array(7);
46+
let i = 0;
47+
let key, val;
48+
const keys = Object.keys(data);
49+
const len = keys.length;
50+
for (key of keys) {
51+
val = data[key];
52+
a[i++] = val;
53+
}
54+
}
55+
56+
function testForOfLet() {
57+
const a = Array(7);
58+
let i = 0;
59+
let key, val;
60+
const keys = Object.keys(data);
61+
const len = keys.length;
62+
for (let key of keys) {
63+
val = data[key];
64+
a[i++] = val;
65+
}
66+
}
67+
68+
benchmark.do(1000000, 5, [
69+
testForKeys,
70+
testForIn,
71+
testForInLet,
72+
testForOf,
73+
testForOfLet
74+
]);

0 commit comments

Comments
 (0)