var array = [4457, 6563, 30851, 40303, 42053, 47405, 52810, 90772, 96252, 125488, 151673, 158557, 178050, 195190, 203029, 210539, 216541, 242156, 262560, 275861, 284045, 285385, 286899, 307581, 337018, 340978, 353359, 365569, 398211, 398284, 424423, 425550, 427665, 429594, 440844, 445754, 452877, 491428, 494809, 501187, 513180, 518540, 520189, 527068, 532607, 537414, 541551, 543608, 544796, 548722, 564979, 569266, 586233, 589433, 604742, 628230, 649872, 651744, 657815, 663902, 669333, 676214, 684405, 705674, 706487, 709528, 728070, 728490, 729012, 741404, 742443, 750860, 760686, 783419, 791554, 797225, 799311, 813491, 816277, 822413, 844859, 865342, 869820, 871814, 873451, 873918, 879036, 882069, 902231, 918049, 918788, 920500, 930129, 932809, 943528, 943818, 964607, 974967, 995708, 999756];
array.indexOf(151673);
array.includes(151673);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
indexOf | |
includes |
Test name | Executions per second |
---|---|
indexOf | 5096661.5 Ops/sec |
includes | 5081301.0 Ops/sec |
I'll break down the provided benchmark JSON and explain what's being tested, compared, and its implications.
Benchmark Definition
The benchmark is designed to measure the performance difference between two JavaScript methods: indexOf
and includes
. Both methods are used to find a specific value within an array. The script preparation code creates a large array of numbers and the test cases use this array.
Test Cases
There are two test cases:
indexOf
method to find the index of a specific number (151673) in the array.includes
method to check if a specific number (151673) is present in the array.Comparison
The comparison between indexOf
and includes
is based on their performance, measured by the number of executions per second.
Options Compared
Two options are being compared:
Pros and Cons of Each Approach:
indexOf()
:includes()
:indexOf()
since it uses a more efficient algorithm that skips over unnecessary elements.Library:
There is no explicit library mentioned in this benchmark. Both indexOf
and includes
are native JavaScript methods built into the language.
Special JS Feature or Syntax:
No special JavaScript features or syntaxes are used in these test cases.
Other Considerations:
When choosing between indexOf
and includes
, consider the following:
indexOf()
.includes()
.Alternatives:
Some alternative approaches for finding specific values within arrays include:
findIndex()
: Returns the index of the first occurrence of the specified value, similar to indexOf()
.indexOf()
or includes()
.