Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values vs filter(Boolean)
(version: 0)
Filtering out undefined values
Comparing performance of:
Object.values vs filter(Boolean)
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var nbspCharacter = '\u00A0'; var GROUPS = [ [ -Infinity, 0, `< 1${nbspCharacter}min` ], [ 1, 4, `1 - 4${nbspCharacter}min` ], [ 5, 14, `5 - 14${nbspCharacter}min` ], [ 15, 29, `15 - 29${nbspCharacter}min` ], [ 30, 59, `30 - 59${nbspCharacter}min` ], [ 60, Infinity, `60${nbspCharacter}min <` ], ]; var entries = []; for(i=0; i<100; i++){ entries.push({minutes: Math.floor(Math.random() * 2), count: Math.floor(Math.random() * 10)}); entries.push({minutes: 30 + Math.floor((Math.random() * 60)), count: Math.floor(Math.random() * 10)}); } var reducer = (acc, entry) => { const { minutes, count } = entry; const groupId = GROUPS.findIndex(([ min, max ]) => { return minutes >= min && minutes <= max; }); acc[groupId] = { x: GROUPS[groupId][2] || (GROUPS[groupId].map((v) => `${v}${nbspCharacter}min`).join(' - ')), count: parseInt((acc?.[groupId]?.count ?? 0), 10) + parseInt(count, 10), }; acc[groupId].y = acc[groupId].count; return acc; }
Tests:
Object.values
Object.values(entries.reduce(reducer, {}))
filter(Boolean)
entries.reduce(reducer, []).filter(Boolean);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.values
filter(Boolean)
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/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.values
16679.7 Ops/sec
filter(Boolean)
16891.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark compares the performance of two JavaScript methods: `Object.values` and `filter(Boolean)`. The benchmark is designed to measure which method is faster when applied to an array of objects. **Options Compared** Two options are compared: 1. **Object.values**: This method returns an array of a given object's own enumerable property values. 2. **filter(Boolean)**: This method creates a new array with all elements that pass the test implemented by the provided function. **Pros and Cons** * **Object.values**: * Pros: Simple, straightforward, and easy to understand. * Cons: May be slower than `filter(Boolean)` for large arrays due to the overhead of creating an object for each element. * **filter(Boolean)**: * Pros: More efficient for large arrays since it only checks if a value is truthy or falsy, which can reduce the number of iterations. However, it may be less intuitive and more error-prone than `Object.values`. * Cons: May have additional overhead due to function calls and array indexing. **Library** None. **Special JS Feature/Syntax** No special features or syntax are used in this benchmark. **Benchmark Preparation Code** The preparation code generates an array of objects (`entries`) with random properties (`minutes` and `count`). The `reducer` function is then defined to group the entries by their `minutes` values. **Individual Test Cases** Two test cases are provided: 1. **Object.values**: Applies `Object.values` to the reduced array of entries. 2. **filter(Boolean)**: Applies `filter(Boolean)` to the reduced array of entries. **Latest Benchmark Result** The latest benchmark result shows the execution times per second for both test cases on a Chrome 122 browser running on a Mac OS X 10.15.7 device: * **filter(Boolean)**: 16891.37890625 executions per second * **Object.values**: 16679.705078125 executions per second **Other Alternatives** Some alternative approaches could be considered for this benchmark, such as: * Using `map` and `reduce` instead of `filter(Boolean)`. * Using a library like Lodash's `pickBy` to achieve the same result. * Using a different method, such as `Array.prototype.forEach`, to group the entries by their `minutes` values. However, these alternatives would likely change the focus or scope of the benchmark and may not be as straightforward or easy to understand as the original approach.
Related benchmarks:
native built-in filter map function vs native logic for filter, map (about huge map)
native built-in filter map function vs native logic for filter, map (about huge map #2)
Test filter map vs only map
_.filter vs array filter - fork
fxjs vs native
Comments
Confirm delete:
Do you really want to delete benchmark?