Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript Filter Object
(version: 2)
Javascript Filter Object
Comparing performance of:
Test 1 vs Test 2
Created:
5 years ago
by:
Registered User
Jump to the latest result
Tests:
Test 1
let obj = { 'a': 1, 'b': 2, 'c': 3 }; let condVal = 2; let result = {}, key; for (key in obj) { if (obj.hasOwnProperty(key) && condVal == obj[key]) { result[key] = obj[key]; } }
Test 2
let obj = { 'a': 1, 'b': 2, 'c': 3 }; let condVal = 2; Object.keys(obj) .filter( key => condVal == obj[key] ) .reduce( (res, key) => (res[key] = obj[key], res), {} );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Test 1
Test 2
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/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Test 1
73504888.0 Ops/sec
Test 2
23872286.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 definition is a JSON object that contains metadata about the benchmark, such as its name and description. It also includes two script preparation codes: 1. The first script creates an object `obj` with properties `'a'`, `'b'`, and `'c'`. It then defines a variable `condVal` and initializes an empty object `result`. The script uses a `for...in` loop to iterate over the properties of `obj` and checks if each property value matches `condVal`. If it does, the corresponding key-value pair is added to the `result` object. 2. The second script is similar to the first one but uses `Object.keys()`, `filter()`, and `reduce()` methods from the JavaScript `Object` prototype. **Test Cases** There are two individual test cases: 1. **Test 1**: This test case uses the first script preparation code. It measures the performance of filtering an object based on a condition, where the condition is applied to each property value. 2. **Test 2**: This test case uses the second script preparation code. It measures the performance of filtering an object using `Object.keys()`, `filter()`, and `reduce()` methods. **Comparison of Approaches** The two approaches differ in their use of JavaScript features: 1. **Looping through properties**: Both scripts use a `for...in` loop to iterate over the properties of the object. 2. **Filtering**: Test 1 uses a simple `if` statement to filter out properties, while Test 2 uses the `filter()` method. 3. **Reducing**: Test 2 also uses the `reduce()` method to accumulate the filtered key-value pairs. **Pros and Cons** Here are some pros and cons of each approach: 1. **Looping through properties**: * Pros: More explicit control over iteration, easier to debug. * Cons: May be slower due to the overhead of the loop, more code to write. 2. **Filtering using `filter()`**: Pros: Concise and efficient, eliminates the need for explicit looping. Cons: May require additional imports or polyfills if not supported by older browsers. 3. **Reducing using `reduce()`**: Pros: Efficient and concise way to accumulate results, can be faster than other approaches due to its optimized implementation. Cons: May require additional imports or polyfills if not supported by older browsers. **Library Usage** Neither test case uses a specific library. However, the use of `Object.keys()`, `filter()`, and `reduce()` suggests that the benchmark is testing JavaScript 1.8+ features. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. **Alternatives** If you were to rewrite these benchmarks using alternative approaches, here are some options: * Use a more concise filtering method, such as `Object.entries().filter(([key, value]) => value === condVal).reduce((result, [key, value]) => ({ ...result, [key]: value }), {})` * Use a different iteration method, such as `for...of` or `forEach()`, which may offer better performance or readability. * Add additional test cases to measure the performance of other JavaScript features or libraries. Note that these alternatives would require changes to the script preparation codes and test case definitions.
Related benchmarks:
Filter: Lodash vs Native
Array.prototype.filter vs Lodash filter
lodash v native filter
Filter: Lodash 2 vs Native
Array.prototype.filter vs Lodash filter 1Million
Comments
Confirm delete:
Do you really want to delete benchmark?