<!--your preparation HTML code goes here-->
function omit(obj, keys) {
return keys.reduce((a, e) => {
const {
[e]: _, rest
} = a;
return rest;
}, obj);
}
function omit2(obj, keys) {
return Object.fromEntries(Object.entries(obj).filter(([k]) => !keys.includes(k)));
}
omit({ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }, ['b']);
omit({ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }, ['b']);
omit({ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']);
omit2({ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
omit 1 | |
omit2 1 | |
omit 8 | |
omit2 8 |
Test name | Executions per second |
---|---|
omit 1 | 6259850.0 Ops/sec |
omit2 1 | 6273293.5 Ops/sec |
omit 8 | 1159204.1 Ops/sec |
omit2 8 | 2237010.0 Ops/sec |
The benchmark described in the provided JSON compares two different implementations of an omit
function in JavaScript. The purpose of these functions is to create a new object by omitting specified keys from an original object. The benchmark tests two approaches to accomplish this task:
Array.prototype.reduce
- Implemented in the omit
function.Object.fromEntries
with filtering - Implemented in the omit2
function.reduce
(omit)Implementation: This method uses the reduce
function on the keys
array to iterate over omitted keys, destructuring the input object to omit specified keys while returning a new object containing the rest of the properties.
function omit(obj, keys) {
return keys.reduce((a, e) => {
const { [e]: _, ...rest } = a;
return rest;
}, obj);
}
Pros:
Cons:
reduce
creates a new object.Object.fromEntries
(omit2)Implementation: This method utilizes Object.entries
to convert the object into an array of key-value pairs, applies filter
to retain entries that are not specified in keys
, and then transforms the filtered array back into an object with Object.fromEntries
.
function omit2(obj, keys) {
return Object.fromEntries(Object.entries(obj).filter(([k]) => !keys.includes(k)));
}
Pros:
Cons:
Object.fromEntries
, and the usage of multiple functional programming methods may confuse some readers.The benchmark comprises four test cases, comparing the performance of both methods across different scenarios:
omit 1 and omit2 1: Both implementations test omitting a single key 'b'
from a sample object with properties from 'a'
to 'j'
. This case assesses how each function performs when the number of keys to omit is minimal.
omit 8 and omit2 8: Both implementations test omitting multiple keys (['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
). This situation examines the performance when many keys are excluded at once.
The collected performance metrics indicate the rates of execution measured in executions per second for each test:
omit2
performs slightly better than omit
by a margin in the single key scenario, which becomes more significant with multiple key omissions.omit2
executed at approximately 6,273,293 operations per second for a single key, compared to omit
at 6,259,850.omit2
executed at around 2,237,010, while omit
was at 1,159,204, illustrating a significant performance advantage as the number of keys increased.omit
provides a clearer path to understanding the logic, omit2
offers performance benefits.Object.fromEntries
is available in ES2019 and later, meaning support on older platforms may require a polyfill, whereas reduce
is widely supported.Besides the approaches tested in the benchmark, alternatives might include:
_.omit
: This utility function presents an elegant way to omit properties from objects but comes with the overhead of including an entire library.This detailed explanation caters to a broad audience of software engineers, whether they specialize in JavaScript or have varied backgrounds in programming. They can utilize the information to make informed decisions around performance-tuning and code maintainability.