Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values on array vs Object
(version: 0)
Is it significantly better to do Object.values(<array or object>) or if( Array.isArray(<array or object>) ) { array = <array or object> } else { Object.values(obj) }
Comparing performance of:
a vs o vs ac vs oc
Created:
6 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var obj = {} for(let i = 0; i < 50; ++i) { let k = String.fromCharCode((i % 26) + 97); obj[k] = i; } var arr = Object.values(obj); var dest = null;
Tests:
a
dest = Object.values(arr);
o
dest = Object.values(obj);
ac
if( Array.isArray(arr) ) { dest = arr; } else { dest = Object.values(arr); }
oc
if( Array.isArray(obj) ) { dest = obj; } else { dest = Object.values(obj); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
a
o
ac
oc
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0
Browser/OS:
Chrome 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
ac
10.7M/s
a
5.6M/s
oc
569K/s
o
561K/s
View exact numbers
Test name
Executions per second
✓
ac
10,675,785 Ops/sec
a
5,649,176 Ops/sec
oc
569,174 Ops/sec
o
561,101 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. **Benchmark Definition** The benchmark compares three different approaches to achieve the same result: 1. `Object.values(arr)` or `Object.values(obj)` 2. `if (Array.isArray(arr)) { dest = arr; } else { dest = Object.values(arr); }` 3. `if (Array.isArray(obj)) { dest = obj; } else { dest = Object.values(obj); }` **What is being tested?** The benchmark tests the performance of these three approaches on arrays and objects. The test creates an array or object with 50 properties, each containing a unique value from 0 to 25 (using ASCII code). **Options compared** Here's a summary of what's being compared: * `Object.values(arr)` vs `if (Array.isArray(arr)) { dest = arr; } else { dest = Object.values(arr); }` + Pros: - Concise and readable - Avoids unnecessary checks for array type + Cons: - May be slower due to the additional check and potential overhead of `Object.values()` * `Object.values(obj)` vs `if (Array.isArray(obj)) { dest = obj; } else { dest = Object.values(obj); }` + Pros: - Similar to option 1, but for objects + Cons: - May be slower due to the additional check and potential overhead of `Object.values()` **Special considerations** There are no special JavaScript features or syntax used in this benchmark. **Alternatives** Other alternatives could include: * Using a library like Lodash or Immutable.js, which provide optimized implementations for array and object manipulation. * Implementing custom optimizations for specific use cases or performance-critical code. * Using native WebAssembly (WASM) or other low-level languages to achieve performance enhancements. Overall, the benchmark provides a useful comparison of three common approaches to accessing values in arrays and objects. By understanding the trade-offs between conciseness, readability, and performance, developers can make informed decisions about how to optimize their code for specific use cases.
Related benchmarks
Object.keys vs Object.values
Reading object values in loop: Object.keys vs Object.values
Array isArray vs typeof
Array isArray vs Object.prototype
Comments
Confirm delete:
Do you really want to delete benchmark?