<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
window.arr = [{
key : 'pier',
object : 'organization',
path : 'customAttributes',
fullPath : 'customAttributes.pier',
dataType : 'string',
label : 'label',
filterable : false,
searchable : true,
sortable : true,
writable : true,
custom : true,
archived : false
}, {
key : 'batang-pier',
object : 'organization',
path : 'customAttributes',
fullPath : 'customAttributes.pier',
dataType : 'string',
label : 'label',
filterable : false,
searchable : true,
sortable : true,
writable : true,
custom : true,
archived : true
}, {
key : 'global-port',
object : 'organization',
path : 'customAttributes',
fullPath : 'customAttributes.pier',
dataType : 'string',
label : 'label',
filterable : false,
searchable : true,
sortable : true,
writable : true,
custom : true,
archived : false
}]
var result = _.reduce(arr, function(result, attribute) {
if (!attribute.archived) {
result[attribute.key] = attribute;
}
return result;
}, {})
console.log(result)
var result = arr.reduce(function(result, attribute) {
if (!attribute.archived) {
result[attribute.key] = attribute;
}
return result;
}, {});
console.log(result)
const result = {};
for (let i = 0; i < arr.length; i++) {
const attribute = arr[i];
if (!attribute.archived) {
result[attribute.key] = attribute;
}
}
console.log(result)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Native | |
For Loop |
Test name | Executions per second |
---|---|
Lodash | 49538.4 Ops/sec |
Native | 50513.1 Ops/sec |
For Loop | 46276.4 Ops/sec |
The provided JSON represents a benchmark test case for comparing the performance of Lodash, native JavaScript, and a for loop in filtering an array of objects.
Benchmark Definition
The test case defines three different approaches:
_.reduce()
method is used to filter the array of objects. This approach relies on the Lodash library.reduce()
method to filter the array of objects. This approach does not rely on any external libraries.Options Comparison
The three approaches have different pros and cons:
Library - Lodash
Lodash is a popular JavaScript library that provides a collection of helper functions for common tasks, including array manipulation. In this test case, the _.reduce()
method is used to filter the array of objects. The Lodash library provides a consistent and predictable output, making it easier to reason about the behavior of the code.
Special JS Feature or Syntax
There are no special JavaScript features or syntax used in this test case. However, the use of the _.reduce()
method from Lodash is worth noting as a feature that allows for concise and expressive filtering logic.
Other Alternatives
In addition to the three approaches mentioned above, other alternatives could be considered:
Array.prototype.filter()
or Array.prototype.map()
methods instead of reduce()
.In summary, the test case compares the performance of three approaches to filter an array of objects: Lodash, native JavaScript, and a for loop. Each approach has its pros and cons, and the choice of which one to use depends on the specific requirements and constraints of the project.