Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Loop over object: lodash.forOwn vs Object.keys().forEach with songbird2
(version: 2)
Comparing performance of:
lodash.forOwn vs Object.keys().forEach vs for..in vs lodash.forOwn (looping subObj) vs Object.keys().forEach (looping subObj)
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script> <script src="https://raw.githubusercontent.com/duereg/songbird/master/lib/songbird.js"></script>
Script Preparation code:
var subObj = Array.from({ length: 10 }).map((value, i) => i).reduce((val, v) => { val[v] = v; return val; }, {}) var obj = Array.from({ length: 1000 }).map((value, i) => i).reduce((val, v) => { val[v] = v; return val; }, {}) var cb = (v, k) => { v.a = 10; v.b = 20; }
Tests:
lodash.forOwn
_.forOwn(obj, function(v, k) { cb(v, k); });
Object.keys().forEach
Object.keys(obj).forEach(function (k) { let v = obj[k]; cb(v, k); });
for..in
for (const k in obj) { if (obj.hasOwnProperty(k)) { let v = obj[k]; cb(v, k); } }
lodash.forOwn (looping subObj)
_.forOwn(obj, function(v, k) { _.forOwn(subObj, function(v1, k1) { cb(v, k); }); });
Object.keys().forEach (looping subObj)
Object.keys(obj).forEach(function (k) { const v = obj[k]; Object.keys(subObj).forEach(function (k1) { const v1 = subObj[k1]; cb(v, k); }); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
lodash.forOwn
Object.keys().forEach
for..in
lodash.forOwn (looping subObj)
Object.keys().forEach (looping subObj)
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 dive into the world of JavaScript microbenchmarks and explore what's being tested in this benchmark. **Benchmark Overview** The benchmark measures the performance of four different approaches to iterate over an object: `lodash.forOwn`, `Object.keys().forEach`, `for..in`, and two variants that loop over a sub-object (`lodash.forOwn (looping subObj)` and `Object.keys().forEach (looping subObj)`). **Options Compared** The benchmark compares the performance of four options: 1. **_lodash.forOwn**: A utility function from the Lodash library that iterates over an object using a callback function. 2. **Object.keys().forEach**: A built-in JavaScript method that iterates over an object's keys and executes a callback function for each key-value pair. 3. **for..in**: A traditional loop that uses `in` to iterate over an object's properties. 4. Two variants: * **_lodash.forOwn (looping subObj)**: Uses the same `_.forOwn` approach but loops over a sub-object (`subObj`) in addition to the original object (`obj`). * **Object.keys().forEach (looping subObj)**: Uses the same `Object.keys().forEach` approach but also loops over a sub-object (`subObj`) in addition to the original object (`obj`). **Pros and Cons** Here's a brief summary of each approach: 1. **_lodash.forOwn**: This method is often faster than `for..in` because it avoids the overhead of checking for property existence using `in`. However, it requires an external library (Lodash). 2. **Object.keys().forEach**: This built-in method is generally faster and more convenient than `for..in`, but it can be slower in certain cases due to its own internal optimizations. 3. **for..in**: This traditional loop is simple and widely supported, but it may be slower than the other methods due to the overhead of checking for property existence. The looping variants (`_lodash.forOwn (looping subObj)` and `Object.keys().forEach (looping subObj)`) add an additional layer of complexity by looping over a sub-object. While they may provide better performance in certain cases, they also increase the codebase's complexity and require more memory to execute. **Library Used** The benchmark uses the following libraries: 1. **Lodash**: A utility library that provides `_.forOwn`, among other functions. 2. **Songbird2**: A JavaScript engine or interpreter (the exact nature of which is unclear) that appears to provide a way to run JavaScript code in isolation. **Special JS Feature/Syntax** This benchmark does not seem to use any special JavaScript features or syntax, such as async/await, promises, or modern ECMAScript specifications. It only uses standard JavaScript constructs like loops and function calls. **Alternatives** If you're interested in exploring alternative approaches for iterating over objects, consider the following options: 1. **Array.prototype.forEach**: A simpler alternative to `Object.keys().forEach` that's available on all arrays. 2. **for..of**: A newer loop syntax (introduced in ECMAScript 2015) that provides a more concise way to iterate over iterable objects. 3. **map() and filter()**: Methods available on arrays that can be used to process data without the need for explicit loops. These alternatives may offer better performance or convenience in certain cases, but they're not directly comparable to the options tested in this benchmark.
Related benchmarks:
Loop over object: lodash vs Object.entries
lodash vs for-of vs forEach5453
Loop over object: lodash vs Object.entries and Object.keys
Loop over object: lodash vs Object.entries [2]
Comments
Confirm delete:
Do you really want to delete benchmark?