-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop-vs-array-indexof.coffee
60 lines (50 loc) · 1004 Bytes
/
loop-vs-array-indexof.coffee
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
bench = require '../bench'
N = 1e6
arr = [1..N]
set = new Set arr
map = new Map arr.map (x) => [x, x]
obj = {}
arr.forEach (x) => obj[x] = x
pattern = null
a = =>
for x in pattern
i = N
while i-- and x isnt arr[i]
undefined
if x is arr[i] then i else -1
b = =>
for x in pattern
arr.indexOf x
forEach = =>
res = []
for x in pattern
arr.forEach (y, i) =>
if x is y
res.push i
false
some = =>
for x in pattern
arr.some (y) => x is y
includes = =>
for x in pattern
arr.includes x
has = =>
for x in pattern
set.has x
get = =>
for x in pattern
map.get x
property = =>
for x in pattern
obj[x]
p = => pattern = ((Math.random() * N) | 0 for i in [1..5])
bench.setPrepare p
.addCase 'while loop', a
.addCase 'Array indexOf', b
.addCase 'Array forEach', forEach
.addCase 'Array some', some
.addCase 'Array includes', includes
.addCase 'Set has', has
.addCase 'Map get', get
.addCase 'Object property', property
.run 100