var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var b = { 1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {}, 8: {}, 9: {}, 10: {} };
return a.includes(9)
return b[9]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
includes | |
lookup |
Test name | Executions per second |
---|---|
includes | 85495864.0 Ops/sec |
lookup | 154475792.0 Ops/sec |
I'd be happy to help explain the provided benchmark!
Benchmark Overview
The MeasureThat.net website is used to create and run JavaScript microbenchmarks, which allows users to compare the performance of different approaches on various devices.
Script Preparation Code
The script preparation code defines two variables:
a
: an array with 10 elements (from 1 to 10)b
: an object with 10 properties, where each property has an empty object as its value. The keys of the object match the values in the a
array.Benchmark Definition
The benchmark definition consists of two test cases:
includes()
method on the a
array to check if 9 is included.b
to access its value at key 9.Comparison Options
The benchmark compares two approaches:
includes()
method on an arrayPros and Cons of each approach:
Library Used:
None, as this is a basic benchmark without any external libraries.
Special JS Features/Syntax:
includes()
method provides a convenient way to check if an array includes a specific value.Other Alternatives:
If you wanted to modify this benchmark to explore other approaches, here are some ideas:
indexOf()
instead of includes()
.I hope this explanation helps you understand the benchmark!