const counter = { val: 0 };
function action() {
counter.val++;
}
function actionWithLog() {
counter.val++;
console.log('ok');
}
function actionWithLogData() {
counter.val++;
console.log('ok', counter);
}
const customLog = [];
function actionWithCustomLog() {
counter.val++;
customLog.push(['ok', {counter}]);
}
action();
actionWithLog();
actionWithLogData();
actionWithCustomLog()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
action | |
actionWithLog | |
actionWithLogData | |
actionWithCustomLog |
Test name | Executions per second |
---|---|
action | 686780352.0 Ops/sec |
actionWithLog | 65731.2 Ops/sec |
actionWithLogData | 56033.8 Ops/sec |
actionWithCustomLog | 5026856.5 Ops/sec |
Let's dive into the details of this benchmark.
What is being tested?
The provided JSON represents four JavaScript microbenchmarks:
action()
: This function simply increments a counter variable (counter.val
) and does nothing else.actionWithLog()
: Similar to action()
, but also logs "ok" to the console using console.log()
.actionWithLogData()
: Also increments the counter and logs "ok" followed by the current state of the counter object (counter
) using console.log()
.actionWithCustomLog()
: Similar to actionWithLog()
, but instead of logging to the console, it pushes an array with the string "ok" and a copy of the counter
object to an external log variable (customLog
).Options being compared
The four test cases are comparing different approaches to incrementing a counter variable:
action()
vs. actionWithLog()
: Does logging affect performance?action()
vs. actionWithLogData()
: Does logging the current state of the object impact performance?action()
vs. actionWithCustomLog()
: How does storing log data in an external array compare to logging directly to the console?Pros and Cons
actionWithLog
):actionWithLogData
):actionWithCustomLog
):Special considerations
The test cases use some standard JavaScript features, including:
console.log()
statementsThere are no notable special features or syntax used in these benchmarks.
Alternatives
If you wanted to create a similar benchmark, you could consider adding more test cases that explore additional scenarios, such as:
Keep in mind that each added test case would require more code and potentially increase the benchmark's overall overhead.