const createItem = (value, index) => ({ id: index, label: "Label" });
const payload = new Array(3).map(createItem);
var action = { payload };
var emptyValues = (source) => {
const copy = {};
// create an object with the same properties but empty values
Object.keys(source).forEach((key) => copy[key] = typeof source[key] === "string" ? "" : null);
return copy;
};
var extractAttributes = (payload) => payload.attributes;
const repository = (payload) => {
const data = payload.map(extractAttributes);
return [
data.slice(0, 1).map(emptyValues),
data
];
}
repository(action.payload);
const repository2 = (payload) => {
return [
payload.slice(0, 1).map(extractAttributes).map(emptyValues),
payload.map(extractAttributes)
];
}
repository2(action.payload);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
using extra data constant | |
without extra constant |
Test name | Executions per second |
---|---|
using extra data constant | 506654.8 Ops/sec |
without extra constant | 446609.3 Ops/sec |
Measuring that.net is a website where users can create and run JavaScript microbenchmarks to compare the performance of different approaches.
Benchmark Definition
The benchmark definition in JSON format represents two test cases:
Both test cases use the same JavaScript function repository
with slightly different implementations.
Script Preparation Code
The script preparation code defines two functions:
createItem
: creates an object with a fixed structure (id and label) for each item in the payload array.payload
: creates an array of three items using the createItem
function.action
: creates an object with the payload as its property.emptyValues
: creates a copy of an object with empty values for non-string properties.extractAttributes
: extracts attributes from an object.Html Preparation Code
There is no HTML preparation code provided, which means that this benchmark does not rely on any specific HTML structure or rendering.
Test Cases
The two test cases differ in the implementation of the repository
function:
extractAttributes
.emptyValues
.extractAttributes
directly on the payload.Pros and Cons
Library
There is no explicit library mentioned in the benchmark definition or test cases.
Special JS Feature
The benchmark uses the ...
operator (spread operator) to concatenate arrays. This feature was introduced in ECMAScript 2015 (ES6).
Alternatives
Other alternatives for benchmarking similar scenarios might include:
repository
function using a different programming paradigm, such as imperative vs. functional programming.repository
function.In general, benchmarking involves identifying and optimizing performance-critical sections of code that can have a significant impact on system performance. Measuring that.net provides a simple and accessible platform for developers to test their ideas and compare different approaches.