<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js'></script>
var data = Object.fromEntries(Array.from({
length: 10000
}, (i) => ['key_' + i, i]));
const output = {}
for(const key of Object.keys(data)) {
const newKey = _.camelCase(key)
output[newKey] = data[key]
}
Object.fromEntries(Object.keys(data).map((key) => [_.camelCase(key), data[key]]))
Object.fromEntries(Object.entries(data).map((entry) => [_.camelCase(entry[0]), entry[1]]))
Object.fromEntries(Object.entries(data).map(([key, value]) => [_.camelCase(key), value]))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for of Object.keys | |
Object.fromEntries with keys | |
Object.fromEntries with entries | |
Object.fromEntries with entries - destruction entry |
Test name | Executions per second |
---|---|
for of Object.keys | 3264554.0 Ops/sec |
Object.fromEntries with keys | 2694602.8 Ops/sec |
Object.fromEntries with entries | 2668345.0 Ops/sec |
Object.fromEntries with entries - destruction entry | 2622282.2 Ops/sec |
The benchmark you've provided compares different methods of transforming keys in an object using JavaScript. The main focus is on how to change the case of the keys from camelCase to underscore_case using the lodash library, specifically its _.camelCase()
method. It involves measuring the performance of different approaches to achieve the same result.
Setup:
data
, which is an object containing 10,000 key-value pairs. The keys are string representations of the format key_0
, key_1
, ..., key_9999
.Benchmark Test Cases:
for of Object.keys:
for...of
loop to iterate over the keys of the object obtained through Object.keys(data)
, applying _.camelCase
to each key, and constructing a new output object.Object.fromEntries with keys:
Object.keys(data)
and transforms it into entries using the map
method to form [key, value]
pairs. It utilizes Object.fromEntries
to create a new object.Object.fromEntries with entries:
Object.entries(data)
which gives both key and value pairs, applying _.camelCase
to the key before passing it to Object.fromEntries
.for of
method and may be less familiar to some developers.Object.fromEntries with entries - destructuring entry:
([key, value]) => [_.camelCase(key), value]
._.camelCase()
function, which converts strings to camelCase format. For instance, if called with 'key_1'
, it would produce 'key1'
.for...of
loop offers the best performance, likely due to less function call overhead compared to higher-order functions such as map
.Object.fromEntries
and Object.keys()
or Object.entries()
provides a more modern approach that can lead to clearer code but may sacrifice some performance.for
loops would also work, but generally have more boilerplate code and are less readable.Choosing the right approach depends on the context of use; for performance-critical applications, one might choose the for of Object.keys
route, whereas for readability and maintainability, Object.fromEntries
with entries is favorable. Each method presents its own trade-offs between performance, readability, and ease of understanding.