<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script>
underscore = _;
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.12.0/lodash.min.js"></script>
<script>
lodash = _;
</script>
</head>
<body>
</body>
</html>
var PromosArray = [
{
type: "regular",
display: "$123.00"
},
{
type: "markdown",
display: "$100.00"
},
{
type: "promo",
display: "$80.00"
}
];
var PromosObject = {
regular: "$123.00",
markdown: "$100.00",
promo: "$80.00"
};
function contains(array, type) {
for (var i = 0; i < array.length; i++) {
if (array[i]["type"] === type) {
return true;
}
}
return false;
}
function testArrayContains() {
contains(PromosArray, "regular");
contains(PromosArray, "markdown");
contains(PromosArray, "promo");
}
function testObjectContains() {
if (PromosObject.regular) {
// skip display logic
}
if (PromosObject.markdown) {
// skip display logic
}
if (PromosObject.promo) {
// skip display logic
}
}
function testUnderscoreContains() {
underscore.some(PromosArray, "type", "regular");
underscore.some(PromosArray, "type", "markdown");
underscore.some(PromosArray, "type", "promo");
}
function testLodashContains() {
lodash.some(PromosArray, "type", "regular");
lodash.some(PromosArray, "type", "markdown");
lodash.some(PromosArray, "type", "promo");
}
testArrayContains();
testObjectContains();
testUnderscoreContains();
testLodashContains();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
testArrayContains | |
testObjectContains | |
testUnderscoreContains | |
testLodashContains |
Test name | Executions per second |
---|---|
testArrayContains | 2071636.6 Ops/sec |
testObjectContains | 3708594.2 Ops/sec |
testUnderscoreContains | 956350.6 Ops/sec |
testLodashContains | 796952.7 Ops/sec |
Measuring the performance of JavaScript code is crucial for optimizing and improving application performance.
The provided benchmark definition JSON represents four test cases that check if certain types exist in arrays and objects.
Array Search Methods
contains
function: This method uses a simple for
loop to iterate through the array and checks if the specified type exists.some
method: This is a popular utility library that provides an efficient way to check if any elements in an array meet a condition.some
method: Similar to the Underscore.js implementation, but from another popular utility library.Object Search Methods
contains
function: Similar to the array search method, but for objects.some
method: Again, using the same optimized implementation as before.Key Takeaways
For array search methods, both the vanilla contains
function and the some
method from Underscore.js or Lodash.js provide good performance. Between these two, Underscore.js might have a slight edge due to its optimized implementation. However, this difference is likely to be negligible in practice.
For object search methods, the vanilla contains
function should be avoided due to its slow performance. The some
method from Underscore.js or Lodash.js is still a good choice, but again, Underscore.js might have a slight edge.
Other Considerations
contains
can be easy to understand and implement, they might not be the best choice for performance-critical parts of your application.Alternative Approaches
If you're looking for alternative approaches, here are a few options:
Ultimately, the best approach will depend on your specific use case and requirements. Be sure to carefully evaluate different options and choose the one that best fits your needs.