<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.29/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>
<script>
_lodash = _.noConflict();
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
// Generate test array
window.array = (function() {
var arr = [];
for (var i = 0; i < 100; ++i) {
arr.push({ name: i, test: i });
}
return arr;
}());
window.quickStringification = (list, propertyKey) => {
let i
let string = ""
for (i = 0; i < list.length; i ++) {
string += list[i][propertyKey]
}
return string
}
JSON.stringify(array)
quickStringification(array, "name")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
JSON stringify | |
Quick stringification |
Test name | Executions per second |
---|---|
JSON stringify | 90326.3 Ops/sec |
Quick stringification | 1293566.8 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Overview
The benchmark measures the performance of two JavaScript functions: JSON.stringify()
and quickStringification()
. Both functions are used to concatenate string values from an array, but they have different approaches and implementations.
JSON.stringify()
JSON.stringify()
is a built-in JavaScript function that converts a value (e.g., an object or an array) into a JSON string. It's implemented in the browser's V8 engine (in this case, Chrome 112).
The benchmark test measures how quickly JSON.stringify()
can be executed with a large array of objects, specifically with the property key "name"
.
quickStringification()
quickStringification()
is a custom JavaScript function that takes an array and a property key as input. It concatenates the values from the array using the provided property key. In this case, it's used to concatenate only the "name"
properties from the array.
The benchmark test measures how quickly quickStringification()
can be executed with the same large array of objects as before.
Library Usage
In the HTML preparation code, several libraries are loaded:
_.noConflict()
): A utility library providing functional programming helpers.However, these libraries are not used directly in the benchmark tests. They might be loaded by the test environment to ensure compatibility or to enable certain features.
Special JS Features/Syntax
There is no specific JavaScript feature or syntax being tested in this benchmark. The tests only involve standard JavaScript functions and array iteration.
Pros and Cons of Different Approaches
In this benchmark, JSON.stringify()
appears to be faster than quickStringification()
likely due to its optimized implementation in the browser's V8 engine. However, the actual performance difference may vary depending on specific use cases and array sizes.
Other Alternatives
If you're looking for alternative approaches to concatenate string values from an array, consider the following:
reduce()
method: arr.reduce((string, obj) => string += obj.name, '')
map()
and `join()```: arr.map(obj => obj.name).join('')
for...of
loop: let string = ''; for (const obj of arr) { string += obj.name; }
These alternatives might offer similar performance to the built-in JSON.stringify()
function, depending on the specific requirements and use cases.