Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.startsWith
(version: 0)
Comparing performance of:
regex.test vs string.includes vs string.startsWith vs startsWithJIT
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "Hello world!"; var regex = /Hello/; function startsWithJitGen(sequence) { const chain = [] chain.push('(typeof value === \'string\')') chain.push(`(value.length >= ${sequence.length})`) for (let i = 0, il = sequence.length; i < il; ++i) { chain.push(`(value[${i}] === '${sequence[i]}')`) } const fnBody = 'return ' + chain.join(' && ') return new Function('value', fnBody) } var startsWithJIT = startsWithJitGen('Hello')
Tests:
regex.test
regex.test(string);
string.includes
string.includes("Hello");
string.startsWith
string.startsWith("Hello");
startsWithJIT
startsWithJIT(string)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
regex.test
string.includes
string.startsWith
startsWithJIT
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
regex.test
12212591.0 Ops/sec
string.includes
30936690.0 Ops/sec
string.startsWith
24669022.0 Ops/sec
startsWithJIT
14318257.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of three different approaches: 1. `string.includes`: checks if a specified substring is present in a string 2. `string.startsWith`: checks if a string starts with a specified prefix 3. `startsWithJIT`: a custom, just-in-time (JIT) compiled version of the `startsWith` function **Options Compared** The benchmark is comparing the performance of these three approaches under different conditions: * The input strings are identical (`"Hello world!"`) * The regular expression pattern for `regex.test` is `/Hello/`, which matches any occurrence of "Hello" anywhere in the string * `startsWithJIT` uses a custom, generated implementation to compile the `startsWith` function at runtime **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. `string.includes`: * Pros: widely supported, easy to implement * Cons: may be slower for larger strings or specific use cases (e.g., exact matching) 2. `string.startsWith`: * Pros: faster than `includes` for shorter strings, can be optimized with caching * Cons: limited to checking if the string starts with a prefix, not suitable for general substring matching 3. `startsWithJIT`: * Pros: potentially much faster than native implementations due to JIT compilation * Cons: requires custom implementation and setup, may not work well for all use cases or edge cases **Library and Custom Implementation** The custom implementation (`startsWithJIT`) uses a library called V8's `Function constructor` to generate and execute the compiled function at runtime. The library is part of the Chrome browser's JavaScript engine. **Special JS Features and Syntax** There are no special JS features or syntax used in this benchmark, as it relies solely on standard JavaScript constructs (strings, loops, conditionals). **Other Alternatives** If you're looking for alternatives to these approaches, consider: * Using `substr` or `indexOf` methods instead of `includes` * Implementing a custom, optimized substring search algorithm using techniques like Knuth-Morris-Pratt (KMP) or Boyer-Moore * Utilizing WebAssembly (WASM) modules or native code interfaces for performance-critical code Keep in mind that the best approach depends on the specific use case and requirements.
Related benchmarks:
RegEx.test vs String.includes vs String.indexOf
RegEx.test vs. String.includes vs. String.indexOf
includes vs regex
RegEx.test (with inline regex) vs. String.includes vs. String.match
RegEx.test vs. String.includes vs. String.match vs String.startsWith
Comments
Confirm delete:
Do you really want to delete benchmark?