-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path9-for-in.js
More file actions
56 lines (49 loc) · 908 Bytes
/
9-for-in.js
File metadata and controls
56 lines (49 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
const benchmark = require('./2-benchmark.js');
const data = {
a: 'abc',
bcd: 'defg',
efgh: 'hijklmn',
ijk: 'opqrst',
lmnopqrs: 'u',
tvuwx: 'v',
yz: 'xyz'
};
const testForKeys = () => {
const a = Array(7);
const keys = Object.keys(data);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
a[i] = data[key];
}
};
const testForIn = () => {
const a = Array(7);
let i = 0;
for (const key in data) {
a[i++] = data[key];
}
};
const testForEach = () => {
const a = Array(7);
let i = 0;
const keys = Object.keys(data);
keys.forEach((key) => {
a[i++] = data[key];
});
};
const testForOf = () => {
const a = Array(7);
let i = 0;
const keys = Object.keys(data);
for (const key of keys) {
const val = data[key];
a[i++] = val;
}
};
benchmark.do(10000000, [
testForKeys,
testForIn,
testForEach,
testForOf,
]);