const b = ['Draft', 'Launcher' , 'InQueue', 'PartlySettled'].includes('InQueue');
const b = 'Draft|Launcher|InQueue|PartlySettled'.includes('InQueue');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
test includes on array | |
test includes on string |
Test name | Executions per second |
---|---|
test includes on array | 141620176.0 Ops/sec |
test includes on string | 223389936.0 Ops/sec |
Let's break down the provided benchmark definition and test cases.
Benchmark Definition
The benchmark is testing the performance of the includes()
function in JavaScript, specifically comparing its performance when used with arrays versus strings.
Script Preparation Code
There is no script preparation code provided, which means that the script to be tested will be generated automatically by MeasureThat.net based on the benchmark definition.
Html Preparation Code
Similarly, there is no html preparation code provided, indicating that the HTML to be executed alongside the JavaScript script will also be generated automatically.
Individual Test Cases
There are two test cases:
b
using an array and checks if the element 'InQueue'
is present in it.const b = ['Draft', 'Launcher' , 'InQueue', 'PartlySettled'].includes('InQueue');
b
using a string with pipes (|
) separating the values and checks if the element 'InQueue'
is present in it.const b = 'Draft|Launcher|InQueue|PartlySettled'.includes('InQueue');
Pros and Cons of Different Approaches
The two approaches tested here are:
includes()
: This approach uses the includes()
function with an array as its argument.includes()
with pipes: This approach uses the includes()
function with a string containing pipes separating the values.Pros and Cons:
includes()
:includes()
only scans the array until it finds the match.includes()
still needs to scan the entire array.includes()
with pipes:split()
function to create an array of values and then use includes()
.Library
None of the test cases uses any external libraries. The JavaScript built-in includes()
function is used in both test cases.
Special JS Features or Syntax
None of the test cases use any special JavaScript features or syntax that are not commonly supported by most browsers.
Other Alternatives
If you want to explore alternative approaches, here are a few options:
findIndex()
instead of includes()
: This can be faster for certain use cases, since it returns the index of the first match, rather than just whether there is a match.In conclusion, the benchmark provided by MeasureThat.net is testing the performance of two different approaches to using the includes()
function in JavaScript: one with arrays and one with strings. The results will help developers understand which approach is faster and more efficient for their specific use cases.