-
Notifications
You must be signed in to change notification settings - Fork 26
/
object-iteration.js
97 lines (84 loc) · 1.68 KB
/
object-iteration.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict'
var benchmark = require('benchmark')
var suite = new benchmark.Suite()
suite.add('for-in', function forIn () {
var obj = {
x: 1,
y: 1,
z: 1
}
var total = 0
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
total += obj[prop]
}
}
})
suite.add('Object.keys functional', function forIn () {
var obj = {
x: 1,
y: 1,
z: 1
}
var total = Object.keys(obj).reduce(function (acc, key) {
return acc + obj[key]
}, 0)
})
suite.add('Object.keys functional with arrow', function forIn () {
var obj = {
x: 1,
y: 1,
z: 1
}
var total = Object.keys(obj).reduce((acc, key) => {
return acc + obj[key]
}, 0)
})
suite.add('Object.keys with for loop', function forIn () {
var obj = {
x: 1,
y: 1,
z: 1
}
var keys = Object.keys(obj)
var total = 0
for (var i = 0; i < keys.length; i++) {
total += obj[keys[i]]
}
})
if (process.versions.node[0] >= 8) {
suite.add('Object.values functional', function forIn () {
var obj = {
x: 1,
y: 1,
z: 1
}
var total = Object.values(obj).reduce(function (acc, val) {
return acc + val
}, 0)
})
suite.add('Object.values functional with arrow', function forIn () {
var obj = {
x: 1,
y: 1,
z: 1
}
var total = Object.values(obj).reduce((acc, val) => {
return acc + val
}, 0)
})
suite.add('Object.values with for loop', function forIn () {
var obj = {
x: 1,
y: 1,
z: 1
}
var vals = Object.values(obj)
var total = 0
for (var i = 0; i < vals.length; i++) {
total += vals[i]
}
})
}
suite.on('complete', require('./print'))
suite.run()