var arr1 = [26151771,
43740574,
63251361,
16664640,
98278385,
77871863,
29846213,
22855546,
73117005,
14272931,
26257507,
48404799,
33962546,
74589143,
65792224,
61526376,
81163137,
62305274,
91767669,
10724813,
72235570,
83204899,
28336814,
43165840,
42682632,
11898964,
86490091,
62257218,
39220214,
36675640,
60842644,
97298952,
66081567,
51168479,
50715262,
65700349,
83417722,
99578496,
25258536,
21667229,
95406635,
15559505,
74866618,
44258359,
31135138,
31518088,
92781538,
25494057,
98326952]
var testArr = [26151771,
43740574,
63251361,
16664640,
98278385,
77871863,
29846213,
22855546,
73117005,
14272931,
26257507,
48404799,
33962546,
74589143,
65792224,
61526376,
81163137,
62305274,
91767669,
10724813,
72235570,
83204899,
28336814,
43165840,
42682632,
11898964,
86490091,
62257218,
39220214,
36675640,
60842644,
97298952,
66081567,
51168479,
50715262,
65700349,
83417722,
99578496,
25258536,
21667229,
95406635,
15559505,
74866618,
44258359,
31135138,
31518088,
92781538,
25494057,
98326952];
var testObj = {
'26151771': true,
'43740574': true,
'63251361': true,
'16664640': true,
'98278385': true,
'77871863': true,
'29846213': true,
'22855546': true,
'73117005': true,
'14272931': true,
'26257507': true,
'48404799': true,
'33962546': true,
'74589143': true,
'65792224': true,
'61526376': true,
'81163137': true,
'62305274': true,
'91767669': true,
'10724813': true,
'72235570': true,
'83204899': true,
'28336814': true,
'43165840': true,
'42682632': true,
'11898964': true,
'86490091': true,
'62257218': true,
'39220214': true,
'36675640': true,
'60842644': true,
'97298952': true,
'66081567': true,
'51168479': true,
'50715262': true,
'65700349': true,
'83417722': true,
'99578496': true,
'25258536': true,
'21667229': true,
'95406635': true,
'15559505': true,
'74866618': true,
'44258359': true,
'31135138': true,
'31518088': true,
'92781538': true,
'25494057': true,
'98326952': true
};
arr1.forEach(item => testArr.find(item))
arr1.forEach(item => testObj[item])
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array find | |
obj access |
Test name | Executions per second |
---|---|
array find | 0.0 Ops/sec |
obj access | 549940.3 Ops/sec |
Let's break down the benchmark definition and options compared.
Benchmark Definition:
The benchmark measures the performance of two different approaches:
arr1.forEach(item => testArr.find(item))
: This approach uses the find
method to search for an item in the testArr
array within a forEach
loop.arr1.forEach(item => testObj[item])
: This approach uses object access to retrieve the value associated with each item in the testObj
object within a forEach
loop.Options compared:
The benchmark compares the performance of these two approaches on a large dataset.
Pros and Cons:
arr1.forEach(item => testArr.find(item))
):find
method.arr1.forEach(item => testObj[item])
):find
, which has a time complexity of O(n).Library/Functionality Used:
forEach
method, which is a built-in JavaScript function.find
method is also a built-in JavaScript function.No special JavaScript features or syntax are mentioned in this benchmark. However, if you're interested in exploring other approaches, we can discuss some alternatives.
Alternative Approaches:
Some alternative approaches to compare with the above methods include:
indexOf
method instead of find
: This can be slightly faster than find
, but may not work correctly for arrays that contain duplicate values.forEach
and find
.If you'd like to explore any of these alternative approaches, feel free to ask!