Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
negation vs equality
(version: 0)
Comparing performance of:
filter with false equality vs filter with negation
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var ops = [true, 1, false, 3, 4, 5, true, "foobar", {}, 3, 4, 5, 6, [], false, 2, true, 2, false, 212, true, 34, 1, 1.909090090909009090909090909, undefined];
Tests:
filter with false equality
ops.filter(op => op === false)
filter with negation
ops.filter(op => !op)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
filter with false equality
filter with negation
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
filter with false equality
18419728.0 Ops/sec
filter with negation
14612296.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what's being tested. **Benchmark Definition** The benchmark is defined in two parts: 1. **Script Preparation Code**: This code defines an array `ops` containing various values, including numbers, strings, objects, booleans, etc. The array is not yet used in any way. 2. **Html Preparation Code**: There is no HTML preparation code provided, so we'll ignore it. The script preparation code is interesting because it's not actually used to test anything. It simply defines an array that will be used later. This might be a leftover from a previous benchmarking setup or a placeholder for future tests. **Individual Test Cases** There are two test cases: 1. **filter with false equality**: The `ops` array is filtered using the expression `op => op === false`. This means that only elements in the array that are equal to `false` will be included in the resulting array. 2. **filter with negation**: The `ops` array is filtered using the expression `op => !op`. This means that only elements in the array that are not equal to their original value (i.e., negated) will be included in the resulting array. **Comparison of Approaches** The two test cases compare the performance of using equality (`===`) versus negation (`!` or `!==`). In general, the JavaScript engine is more efficient when comparing values directly, as it can use optimized instructions and cache information. On the other hand, using negation requires additional computation to negate the value, which may lead to slower performance. **Pros and Cons** * **Equality (===)**: + Pros: faster execution, cache-friendly + Cons: more memory accesses, potentially slower due to branching * **Negation (! or !==)**: + Pros: less memory accesses, potentially faster due to caching + Cons: slower execution, more complex computation **Libraries and Special JS Features** None of the test cases use any libraries or special JavaScript features. The code is a simple array filter with basic arithmetic operations. **Other Alternatives** If you wanted to test this benchmark using different approaches, here are some alternatives: * Compare using `==` instead of `===` * Use a more complex filtering expression, such as `op => op === false && op !== 0` * Introduce additional noise into the array, such as duplicate values or random numbers * Test with a larger input size, such as an array with thousands of elements Keep in mind that these alternatives might not accurately represent real-world scenarios and could potentially skew the results.
Related benchmarks:
Testing for false vs === undefined vs hasOwnProperty for undefined member
Negation vs normal boolean value
undefined equality vs negation
filter null vs negation
Comments
Confirm delete:
Do you really want to delete benchmark?