Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filter + join vs while + str
(version: 0)
Comparing performance of:
case 1 vs case 2
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = ['text', 'text', false && 'text', true && 'text', 'text', undefined, null, 'last str']; var repeats = 10000; var cnF = (...args) => { return args.filter(cls => ( cls !== undefined && cls !== null && cls !== false )).join(' ') }; var cnW = (...args) => { var i = 0 var str = '' while (i < args.length) { var cls = args[i] if (cls !== undefined && cls !== null && cls !== false) { if (str.length > 0) str += ' ' str += cls } i++ } return str };
Tests:
case 1
cnF(arr)
case 2
cnW(arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
case 1
case 2
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
case 1
1121651.0 Ops/sec
case 2
1189313.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and explain what is tested, compared, and their pros and cons. **Benchmark Definition** The benchmark defines two functions: `cnF` (short for "conjugate negative") and `cnW` (short for "conjugate not white"). These functions are designed to filter an array of strings and then join them together. The filtering condition is the same for both functions: only non-undefined, non-null, and non-false values are considered. **Options Compared** The two options being compared are: 1. `cnF` (Conjugate Negative): This function uses the `filter()` method to remove elements that do not meet the filtering condition. 2. `cnW` (Conjugate Not White): This function uses a `while` loop to iterate through the array, checking each element against the filtering condition. **Pros and Cons** * **Filter() Method (cnF)**: + Pros: - More concise and readable code - Less prone to errors due to its functional programming style + Cons: - May be slower for very large arrays, as it creates a new array with filtered elements - Can be less efficient due to the overhead of creating and iterating over the filtered array * **While Loop (cnW)**: + Pros: - More control over iteration order and filtering process - May be faster for very large arrays, as it avoids the overhead of creating a new array + Cons: - More verbose and harder to read code - Prone to errors due to its imperative programming style **Library Usage** There is no explicit library usage in this benchmark. However, the `filter()` method used by `cnF` relies on the built-in JavaScript `Array.prototype.filter()` method. **Special JS Feature or Syntax** The benchmark uses a common JavaScript feature called "short-circuit evaluation" for the `false && 'text'` and `true && 'text'` expressions. This allows the compiler to optimize these expressions away, as they will always evaluate to their respective string values. **Other Alternatives** Other alternatives for filtering arrays in JavaScript include: * Using a regular expression with the `test()` method (e.g., `/^\S+$/`.test(cls)`) * Using a `forEach` loop with a callback function (e.g., `arr.forEach((cls, index) => { ... })`) * Using a library like Lodash's `filterBy` function It's worth noting that the choice of filtering method depends on the specific use case and performance requirements. In general, concise and readable code is preferred for most applications, while performance-critical sections may require more specialized optimizations.
Related benchmarks:
Array filter vs. for loop (fixed really really)
Array filter vs. for loop - with for in
Array filter vs. for loop - with for in 2
filter vs. for
filter + join vs for + concat
Comments
Confirm delete:
Do you really want to delete benchmark?