Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String equals vs String.includes
(version: 0)
how much of a performance deficit you can expect from using String.includes instead of using strict comparison with the === operator
Comparing performance of:
String.includes vs Or chain
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var stringToMatch = 'hello';
Tests:
String.includes
'hello'.includes(stringToMatch)
Or chain
stringToMatch !== 'hello'
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
String.includes
Or chain
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/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String.includes
31562044.0 Ops/sec
Or chain
31747088.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition:** The benchmark measures the performance difference between using the `===` operator for strict comparison and the `includes()` method to check if a string contains another substring. The goal is to determine how much of a performance deficit you can expect from using `String.includes()` instead of `===`. **Script Preparation Code:** ```javascript var stringToMatch = 'hello'; ``` This code sets a variable `stringToMatch` with the value `'hello'`. This variable will be used in both test cases. **Html Preparation Code:** None. The benchmark doesn't require any HTML preparation code, which means it's focused on JavaScript execution performance only. **Test Cases:** There are two individual test cases: 1. **String.includes:** ```javascript 'hello'.includes(stringToMatch) ``` This test case uses the `includes()` method to check if `'hello'` contains the value of `stringToMatch`. The `includes()` method returns `true` if the string contains the specified value, and `false` otherwise. 2. **Or chain:** ```javascript stringToMatch !== 'hello' ``` This test case uses a simple assignment followed by a negation (`!==`) to check if `stringToMatch` is not equal to `'hello'`. This approach is often referred to as an "or chain" or "ternary comparison". **Library and Purpose:** There are no external libraries used in this benchmark. The `includes()` method is a built-in JavaScript method. **JavaScript Feature/Syntax:** The benchmark uses the `includes()` method, which is a relatively modern feature introduced in ECMAScript 2015 (ES6). This method provides a more concise and readable way to check if a string contains another substring. In terms of pros and cons: Pros of using `includes()`: It's shorter and more readable than traditional string comparison methods like `indexOf()` or `substr()`. Cons: Some older browsers may not support this method, and it might be slower in certain cases due to the overhead of function calls. **Alternative Approaches:** Other alternatives for string comparison could include: * Using `indexOf()`: `stringToMatch.indexOf('hello') === -1` * Using `substr()` or `slice()`: `stringToMatch.indexOf('hello', 0) > -1` or `stringToMatch.substring(5, 10) === 'ello'` * Using a custom implementation using bitwise operations (less common and not recommended) The "or chain" approach mentioned in the test case is generally considered more efficient than using `includes()` because it avoids the overhead of function calls. However, it's also less readable and may be less suitable for complex string comparisons. Overall, this benchmark helps evaluate the performance difference between these two approaches to string comparison, providing insights into how to optimize JavaScript code for better performance.
Related benchmarks:
equality vs includes
Compare Or vs Includes
=== vs includes
equals vs includes (one value)
Comments
Confirm delete:
Do you really want to delete benchmark?