Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array filtering v3
(version: 3)
Compare array filtering: - Boolean - x => !!x - x => x - delete + .flat - own preserve length - own
Comparing performance of:
Boolean vs x => !!x vs x => x vs delete + .flat vs own - preserve length vs own
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = new Array(1000).fill(undefined) const falsy = [false, "", 0, null, undefined] const truthy = [true, "string", 10, {}, []] for (let i = arr.length; i--;) { const useFalsy = Math.random() < 0.5 const source = useFalsy ? falsy : truthy const index = Math.floor(Math.random() * source.length) arr[i] = source[index] }
Tests:
Boolean
arr.filter(Boolean)
x => !!x
arr.filter(x => !!x)
x => x
arr.filter(x => x)
delete + .flat
for (let i = arr.length; i--;) if (!arr[i]) delete arr[i] arr.flat()
own - preserve length
const {length} = arr , filtered = new Array(length) let count = 0 for (let i = 0; i < length; i++) { const val = arr[i] if (val) filtered[count++] = val } filtered.length = count
own
const {length} = arr , filtered = [] let count = 0 for (let i = 0; i < length; i++) { const val = arr[i] if (val) filtered[count++] = val }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Boolean
x => !!x
x => x
delete + .flat
own - preserve length
own
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided JSON benchmark definition and individual test cases to help understand what's being tested, compared, and their pros/cons. **Benchmark Definition** The benchmark measures array filtering performance across different approaches: 1. `arr.filter(Boolean)`: Uses the `Boolean` function as a predicate. 2. `x => !!x`: Uses a callback function with a negation operator (`!!`) to determine truthy values. 3. `x => x`: Uses a simple equality check for truthy values (i.e., non-falsy values). 4. `delete + .flat`: Uses the `delete` keyword to remove falsy values from the array and then flattens it using the `.flat()` method. 5. `own - preserve length`: Preserves the original array's length when filtering out falsy values, potentially using a different approach (not explicitly specified). 6. `own`: The base case without any modifications. **Library Usage** The benchmark uses the following library: * None explicitly mentioned in the provided JSON; however, some syntax is specific to certain browsers or versions (more on this later). **Special JS Features/Syntax** This benchmark doesn't use any special JavaScript features or syntax that would require a deep understanding of modern JavaScript. However, it does rely on browser-specific details: * The `Boolean` function as a predicate is supported by most modern browsers. * The negation operator (`!!`) is used to check for truthy values; this is not part of the standard JavaScript specification but is widely supported in modern browsers. * Browser-specific implementations might optimize the callback functions differently, which could impact performance. **Approach Comparison and Pros/Cons** Here's a brief analysis of each approach: 1. `arr.filter(Boolean)`: This method uses the `Boolean` function as a predicate, which can be slower than native truthy checks in some cases. * Pros: Easy to read, reliable across browsers. * Cons: May incur additional overhead for unnecessary comparisons. 2. `x => !!x`: The negation operator (`!!`) is used to check for truthy values. This approach might be faster since it directly performs the negation without needing a separate function call. * Pros: Potentially faster than using `Boolean`. * Cons: May have varying performance across browsers due to implementation differences. 3. `x => x`: Simple equality checks are used, which can be efficient but may not provide the same level of optimization as the first two approaches. * Pros: Fast and straightforward. * Cons: Might require additional comparisons for falsy values. 4. `delete + .flat`: This approach uses a combination of `delete` and `.flat()` methods to filter out falsy values and flatten the array, which can be efficient but also complex. * Pros: Can handle complex filtering scenarios efficiently. * Cons: May have higher overhead due to method calls and memory management. **Other Alternatives** For more advanced filtering scenarios or performance-critical code, consider using: 1. `Array.prototype.filter()`: The native array method is generally the most efficient way to filter arrays in modern JavaScript environments. 2. `Boolean`-free approaches: Depending on the specific requirements, you might be able to use a more efficient approach that avoids unnecessary function calls or comparisons. Keep in mind that this benchmark focuses on array filtering performance and doesn't account for potential edge cases or memory management implications.
Related benchmarks:
Array duplicate removal for duplicates exceeding `N`-number
Filter a list to remove all duplicate values
Filter vs Set (unique)
Remove array items: FindIndex + Splice vs Filter
filter vs splice and indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?