Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
isEmpty (forIn vs Object.keys vs Object.values vs lodash.isEmpty)
(version: 0)
Comparing performance of:
forIn vs lodash vs objectKeys vs objectValues
Created:
5 years ago
by:
Guest
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 Preparation code:
window.emptyArray = []; window.emptyObject = {}; window.emptyString = ''; window.filledArray = []; window.filledObject = {}; window.filledString = ''; Array.from((Array(100).keys())) .forEach((result, i) => { filledArray.push(0); filledObject[i] = 0; filledString[i] += '0'; }); window.forIn = source => { for(var prop in source) { return false; } return true; }; window.lodash = _.isEmpty; window.objectKeys = source => Object.keys(source).length === 0; window.objectValues = source => Object.values(source).length === 0;
Tests:
forIn
forIn(emptyArray); forIn(emptyObject); forIn(emptyString); forIn(filledArray); forIn(filledObject); forIn(filledString);
lodash
lodash(emptyArray); lodash(emptyObject); lodash(emptyString); lodash(filledArray); lodash(filledObject); lodash(filledString);
objectKeys
objectKeys(emptyArray); objectKeys(emptyObject); objectKeys(emptyString); objectKeys(filledArray); objectKeys(filledObject); objectKeys(filledString);
objectValues
objectValues(emptyArray); objectValues(emptyObject); objectValues(emptyString); objectValues(filledArray); objectValues(filledObject); objectValues(filledString);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
forIn
lodash
objectKeys
objectValues
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0
Browser/OS:
Firefox 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forIn
130009.5 Ops/sec
lodash
381082.0 Ops/sec
objectKeys
425605.7 Ops/sec
objectValues
727337.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what is being tested. **Benchmark Overview** MeasureThat.net is testing four different ways to check if an object is empty: using `for...in`, using Lodash's `isEmpty` function, using `Object.keys()`, and using `Object.values()`. **Options Compared** The benchmark compares the performance of these four approaches in three scenarios: 1. Checking if an empty array (`window.emptyArray = []`) 2. Checking if an empty object (`window.emptyObject = {}`) 3. Checking if an empty string (`window.emptyString = ''`) **Approaches and Pros/Cons** Here's a brief overview of each approach, along with their pros and cons: 1. **for...in**: This is the traditional way to iterate over an object's properties in JavaScript. However, it also returns the property names, which may not be what you want if you're only interested in checking if the object is empty. * Pros: Simple to implement, widely supported. * Cons: Can return unnecessary results (property names), slower than other methods for large objects. 2. **Lodash's `isEmpty` function**: This function takes an object as input and returns a boolean indicating whether it's empty or not. * Pros: Fast, accurate, and easy to use. * Cons: Requires Lodash library to be included in the test environment. 3. **Object.keys()**: This method returns an array of property names for an object. You can then check if this array is empty to determine if the object is empty. * Pros: Fast, accurate, and doesn't require Lodash. * Cons: Returns an array of property names, which may be less efficient than checking individual properties. 4. **Object.values()**: This method returns a direct enumeration of a given object's own enumerable property values. * Pros: Fast, accurate, and doesn't require Lodash. * Cons: Requires modern JavaScript versions to work (ES6+), slower for very large objects. **Library: Lodash** Lodash is a popular utility library for JavaScript that provides a wide range of functions for common tasks, such as string manipulation, array manipulation, and object manipulation. In this benchmark, the `isEmpty` function from Lodash is used to check if an object is empty. **Special JS Feature: Arrow Functions** The provided code uses arrow functions (e.g., `filledArray.push(0)`) which were introduced in ECMAScript 2015 (ES6). These functions provide a more concise way of defining small, single-purpose functions. They are widely supported by modern browsers and JavaScript engines. **Other Alternatives** If you're interested in alternative ways to check if an object is empty, here are a few: * Using `Object.getOwnPropertyNames()` instead of `Object.keys()` * Using `for...of` loop with the `entries()` method instead of `Object.keys()` * Using a library like Underscore.js or jQuery to perform the check Keep in mind that these alternatives may have slightly different performance characteristics and use cases compared to the methods tested in this benchmark.
Related benchmarks:
_.isEmpty vs Object.keys.length
_.isEmpty() vs Object.keys().length empty objects
_.isEmpty vs Object.keys.length vs Object.values.length
Object no keys vs isEmpty
_.isEmpty vs Object.keys.length 222
Comments
Confirm delete:
Do you really want to delete benchmark?