<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
<script src=''>
</script>
var omit1 = (originalObj = {}, keysToOmit = []) =>
Object.fromEntries(
Object.entries(originalObj)
.filter(([key]) => !keysToOmit.includes(key))
)
var omit2 = new Function('obj', 'if (!obj) return {}; const { a, d, i, ...res } = obj; return res;');
var omit3 = (originalObject = {}, keysToOmit = []) => {
const clonedObject = { originalObject };
for (const path of keysToOmit) {
delete clonedObject[path]
}
return clonedObject;
}
function omit4(obj, keys) {
var target = {};
for (var i in obj) {
if (keys.indexOf(i) >= 0) continue;
if (!Object.prototype.hasOwnProperty.call(obj, i)) continue;
target[i] = obj[i];
}
return target;
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
var data = [];
for (let i = 0; i < 10000; i++) {
const obj = {};
const propsCount = getRandomInt(0, 10);
for (let j = 0; j < propsCount; j++) {
const propCode = getRandomInt(97, 122);
obj[String.fromCharCode(propCode)] = propCode;
}
data.push(obj);
}
data.map((obj) => _.omit(obj, ['a','d','i']))
data.map((obj) => omit1(obj, ['a','d','i']))
data.map((obj) => omit2(obj, ['a','d','i']))
data.map((obj) => omit3(obj, ['a','d','i']))
data.map((obj) => omit4(obj, ['a','d','i']))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
es omit | |
compiled omit | |
rest and delete omit | |
babel |
Test name | Executions per second |
---|---|
Lodash | 28.8 Ops/sec |
es omit | 91.9 Ops/sec |
compiled omit | 56.3 Ops/sec |
rest and delete omit | 129.2 Ops/sec |
babel | 73.8 Ops/sec |
I'll break down the test cases and explain what's being tested, along with their pros and cons.
Test Cases:
omit
function, which removes specified properties from an object. The _.omit
function is called for each object in the data
array, passing an array of keys to omit (['a', 'd', 'i']
). This test measures how fast Lodash can perform this operation.Pros: Lodash is a widely-used and well-maintained library, so its implementation is likely efficient. Cons: Since it's a third-party library, there might be some overhead in loading and executing it.
omit
function implemented using ES6 syntax (notably, arrow functions and destructuring). The function takes an object and an array of keys to omit as arguments.Pros: This implementation is likely to be efficient since it's written in native JavaScript. Cons: Since it's not part of a library or framework, there might be some overhead in loading and executing this code.
omit
function that has been compiled using a JavaScript compiler (in this case, possibly Babel). The function is similar to the ES6 implementation but uses a different syntax.Pros: Compiling the code might improve its performance by optimizing the generated machine code. Cons: There's still some overhead in loading and executing the compiled code.
omit
function that uses rest properties (...
) to extract the omitted properties and then deletes them from the original object using delete
.Pros: This implementation is likely to be efficient since it avoids creating new objects or arrays. Cons: It might not work in older browsers that don't support rest properties.
omit
function implemented using Babel's syntax (notably, class expressions and arrow functions). The function takes an object and an array of keys to omit as arguments.Pros: This implementation is likely to be efficient since it's written in native JavaScript. Cons: Since it's not part of a library or framework, there might be some overhead in loading and executing this code.
Library/ Framework Considerations:
omit
functions, which might improve their performance by optimizing the generated machine code.Additional Considerations:
ExecutionsPerSecond
column), which gives an idea of how fast each implementation can process the data.Overall, these test cases provide a good comparison between different implementations and libraries/frameworks for removing properties from objects.