Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
comperizon
(version: 0)
Comparing performance of:
includes vs if
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var test='NEW'
Tests:
includes
['NEW', 'OLD'].includes(test);
if
test === 'NEW' || test === 'OLD'
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
if
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
includes
26313874.0 Ops/sec
if
30951404.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark test. The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The test measures the performance difference between two approaches: using the `===` operator for strict equality, and using the `includes()` method for substring detection. **Script Preparation Code** ```javascript var test = 'NEW'; ``` This line declares a string variable `test` with the value `'NEW'`. This variable will be used to compare with different values in the benchmark tests. **Html Preparation Code** This field is empty, indicating that no HTML code needs to be prepared for this benchmark. **Individual Test Cases** There are two test cases: 1. **Test Case 1: includes** ```javascript "Benchmark Definition": "['NEW', 'OLD'].includes(test);", ``` This test case uses the `includes()` method to check if the string `'NEW'` is a substring of the array `['NEW', 'OLD']`. The `includes()` method checks if a specified value exists within an array, and returns a boolean result. In this case, since `'NEW'` is present in the array, the method will return `true`. Pros of using `includes()`: It's a concise way to check for substring presence, and it works even if the array contains duplicate values. Cons: Depending on the browser and JavaScript engine implementation, `includes()` might perform slower than direct comparison using `===`. Additionally, some older browsers may not support this method. 2. **Test Case 2: if** ```javascript "Benchmark Definition": "test === 'NEW' \r\n\t|| test === 'OLD' ", ``` This test case uses a traditional `if` statement to compare the value of `test` with two string literals: `'NEW'` and `'OLD'`. The expression checks both conditions, so if either of them is true, the `if` statement will execute. Pros of using `if`: It's a more explicit way of checking multiple values, making it easier to read and understand. However, it can be slower due to the overhead of evaluating multiple conditions. Cons: As mentioned earlier, some older browsers might not support this syntax. **Library Usage** There is no library explicitly used in these benchmark tests. **Special JS Features/Syntax** The `includes()` method was introduced in ECMAScript 2015 (ES6) and is widely supported by modern JavaScript engines. The `===` operator for strict equality has been part of the language since its inception. **Alternative Approaches** Other ways to check if a string is present in an array include: 1. Using `indexOf()` method: This method returns the index of the first occurrence of the specified value, or -1 if it's not found. 2. Using a custom function with loops: You can write a custom function that iterates through the array and checks for substring presence. Here's an example using `indexOf()`:```javascript function includes(str, arr) { return arr.indexOf(str) !== -1; } ``` Keep in mind that while these alternative approaches might be interesting from a learning perspective, they're not typically used in production code due to performance considerations.
Related benchmarks:
>0 vs !!
Boolean vs !!2
Boolean vs !!3
Check mytest
Comments
Confirm delete:
Do you really want to delete benchmark?