<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var n = 10000;
var o = {};
while (n) {
o["entry"+ n] = true;
n--;
}
_.omit(o, "entry1", "entry2", "entry3", "entry4444", "entry5", "entry6", "entry7777", "entry8", "entry9999", "entry1000");
const {entry1, entry2, entry3, entry4444, entry5, entry6, entry7777, entry8, entry9999, entry1000, props} = o;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash Omit | |
ES6 spread |
Test name | Executions per second |
---|---|
Lodash Omit | 420.0 Ops/sec |
ES6 spread | 279.6 Ops/sec |
Let's dive into the world of MeasureThat.net and analyze the provided benchmark.
Benchmark Definition JSON
The first part, {"Name": "Lodash omit Vs. es6 rest spread random keys"
, explains that this is a comparison between two approaches:
_.omit(o, ...)
).const {entry1, entry2, ...} = o;
).Benchmark Preparation Code
The script preparation code creates a large object o
with 10,000 properties and randomly assigns a value to each property. This is done using a while
loop that decrements a counter n
, assigning the string "entry"+ n
as a key in the object.
Html Preparation Code
This line includes the Lodash library from a CDN, making it available for use in the benchmark.
Individual Test Cases
The two test cases are:
_.omit(o, ...)
function with an array of 10 keys to remove from the object.Pros and Cons
Lodash Omit
Pros:
Cons:
ES6 Spread (Rest)
Pros:
Cons:
Library: Lodash
Lodash is a popular utility library for JavaScript that provides various functions for tasks like array manipulation, object creation, and more. In this benchmark, Lodash is used for the _.omit()
function, which removes specified properties from an object.
Special JS Feature: ES6 Spread (Rest)
The ES6 spread syntax (const {entry1, entry2, ...} = o;
) allows extracting multiple values from an object into separate variables. This feature was introduced in ECMAScript 2015 and is commonly used for destructuring assignment.
Now that we've analyzed the benchmark, here are some other alternatives you might consider:
Object.create()
to create a new object with only the desired properties removed.Keep in mind that these alternatives may not be as efficient or readable as the original benchmark.