var array1 = [0,1,3,532,32,3576,745,1235,6432,31265,654,43212,354,4667,875,999,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991,9991]
array1.indexOf(31265) > -1
array1.includes(31265)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Includes | |
IndexOf |
Test name | Executions per second |
---|---|
Includes | 32452632.0 Ops/sec |
IndexOf | 32923262.0 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript microbenchmarking test on MeasureThat.net. The benchmark compares the performance of two methods to check if an element exists in an array: indexOf
and includes
.
What is being tested?
Two individual test cases are defined:
array1.includes(31265)
returns a boolean value indicating whether 31265 is present in the array.array1.indexOf(31265)
returns an index value (or -1 if not found) indicating the position of 31265 in the array.Options compared
The two methods are compared in terms of their execution time, with the goal of identifying which one is faster for this specific use case.
Pros and Cons of each approach
includes
methodindexOf
due to the overhead of string comparison.indexOf
methodLibrary usage
There is no explicit library mentioned in the benchmark definition. However, array1
is a JavaScript array created using the default var
syntax, which is a part of the ECMAScript standard.
Special JS feature/syntax
The test does not explicitly use any special JavaScript features or syntax that would require additional explanation. Both methods are widely supported and should work in most modern browsers.
Other alternatives
If you wanted to implement this benchmark yourself, you could create an array array1
with a large number of elements and then measure the execution time of both includes
and indexOf
methods using a timer or profiling tool. You would also need to consider any platform-specific differences in behavior or performance.
In JavaScript, alternative approaches for similar use cases might include:
includes()
method on other data structures like strings or sets.