var string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
string.indexOf('tempor123') > -1
string.includes('tempor123')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
IndexOf | |
Includes |
Test name | Executions per second |
---|---|
IndexOf | 15893886.0 Ops/sec |
Includes | 5597300.0 Ops/sec |
I'd be happy to explain what's being tested in the provided JSON benchmark.
Benchmark Purpose
The test aims to measure and compare the performance of two string comparison methods: indexOf
and includes
. The focus is on determining which method is faster when searching for a specific substring within a large string.
Options Compared
Two options are compared:
string.indexOf('tempor123') > -1
: This uses the indexOf
method, which returns the index of the first occurrence of the specified value (in this case, 'tempor123'), or -1 if it's not found.string.includes('tempor123')
: This uses the includes
method, which returns a boolean value indicating whether the string includes the specified value ('tempor123').Pros and Cons
indexOf
Method:includes
Method:indexOf
due to the overhead of searching for substring presence.Library
The test uses the built-in JavaScript String.prototype.indexOf()
and String.prototype.includes()
methods, which are part of the ECMAScript standard. These methods are implemented by most modern browsers and Node.js environments.
Special JS Feature/Syntax (None in this case)
There's no use of special JavaScript features or syntax like async/await, promises, or ES6+ features that require specific browser support.
Other Alternatives
Some alternative approaches to benchmarking string comparison performance include:
Keep in mind that these alternative approaches might require additional setup and configuration to ensure accurate results.