Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values vs for in loop vs for loop v2
(version: 0)
Comparing performance of:
mapForIn vs mapValues vs for loop
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const obj = {}; for (let i = 0; i <= 10000; i++) { obj[i] = { id: i }; }; const mapForIn = () => { const arr = []; for (let key in obj) { if (Object.hasOwnProperty(obj)) { arr.push(obj[key]); } } return arr; } const mapValues = () => Object.values(obj) const mapFor = (func) => { const arr = []; const keys = Object.keys(obj); const numberOfKeys = keys.length; for (let i = 0; i >= numberOfKeys; i++) { arr[i] = obj[i]; }; return arr; };
Tests:
mapForIn
const obj = {}; for (let i = 0; i <= 10000; i++) { obj[i] = { id: i }; }; const mapForIn = () => { const arr = []; for (let key in obj) { if (Object.hasOwnProperty(obj)) { arr.push(obj[key]); } } return arr; } mapForIn();
mapValues
const obj = {}; for (let i = 0; i <= 10000; i++) { obj[i] = { id: i }; }; const mapValues = () => Object.values(obj) mapValues();
for loop
const obj = {}; for (let i = 0; i <= 10000; i++) { obj[i] = { id: i }; }; const mapFor = (func) => { const arr = []; const keys = Object.keys(obj); const numberOfKeys = keys.length; for (let i = 0; i <= numberOfKeys; i++) { arr[i] = obj[i]; }; return arr; }; mapFor();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
mapForIn
mapValues
for loop
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):
Let's break down the provided JSON that represents the JavaScript microbenchmark. **Benchmark Overview** The benchmark measures the performance of three different approaches to iterate over an object: 1. `mapForIn`: Using the `for...in` loop with `Object.hasOwnProperty` 2. `mapValues`: Using `Object.values()` method 3. `for loop v2`: A custom implementation using a traditional `for` loop **Options Comparison** The options are compared in terms of their execution speed, measured in executions per second. * **Pros and Cons:** + `mapForIn`: - Pros: Native JavaScript support, concise code. - Cons: Can be slower due to the overhead of checking `Object.hasOwnProperty`. + `mapValues`: - Pros: Simple, fast, and modern API. - Cons: May not work with older browsers or environments that don't support `Object.values()`. + `for loop v2`: - Pros: Customizable indexing, may be faster due to fewer overhead checks. - Cons: Less readable, more boilerplate code. **Library Usage** The benchmark uses the `Object.keys()` and `Object.values()` methods, which are part of the modern JavaScript API. These methods were introduced in ECMAScript 2015 (ES6) and provide a convenient way to iterate over object properties. **Special JS Features or Syntax** None mentioned in the provided JSON. However, it's worth noting that the use of `for...in` loop with `Object.hasOwnProperty` might be considered non-standard or outdated by some developers. **Alternative Approaches** Other approaches to iterate over an object could include: * Using `Array.prototype.forEach()` * Implementing a custom iterator function * Using a library like Lodash's `_.values()` or `_.keys()` These alternatives would likely have their own pros and cons, depending on the specific use case and requirements. In summary, the benchmark compares three different approaches to iterate over an object: `mapForIn`, `mapValues`, and a custom implementation using a traditional `for` loop. The choice of approach depends on factors such as performance, readability, and compatibility with older browsers or environments.
Related benchmarks:
Object.values vs for in loop
Object.values vs for in loop vs for loop
Object.values vs for in loop vs for loop v1 borys
Object.values vs for in loop vs for loop v2 borys
Comments
Confirm delete:
Do you really want to delete benchmark?