try {
throw new Error(`Err ${Date.now()}`);
}
catch (e) {
console.log(e.stack);
}
try {
console.log(`Hello World ${Date.now()}`);
}
catch (e) {
console.log(`Bye ${Date.now()}`);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Try Catch Throw | |
Console |
Test name | Executions per second |
---|---|
Try Catch Throw | 38755.6 Ops/sec |
Console | 81311.9 Ops/sec |
Let's dive into the world of MeasureThat.net and understand what's being tested in this benchmark.
What is being tested?
MeasureThat.net provides a platform for users to create and run JavaScript microbenchmarks. The provided JSON represents two individual test cases, each containing a "Benchmark Definition" that defines how to measure certain aspects of JavaScript execution.
The first test case, "Try Catch Throw," measures the cost of generating stack traces using try
-catch
blocks with an Error
object thrown inside. This includes factors like memory allocation, CPU usage, and overall performance impact on the engine.
The second test case, "Console," measures the cost of executing JavaScript code that logs messages to the console, including formatting strings with dates (Date.now()
).
Options compared
In both cases, MeasureThat.net is comparing different approaches to measure these specific aspects. The options being compared include:
try
-catch
blocks versus logging directly to the consoleThese comparisons aim to identify which approach has a lower performance impact on the JavaScript engine.
Pros and Cons
When it comes to measuring the cost of generating stack traces, try
-catch
blocks are generally considered more efficient because they:
However, this approach also has some limitations:
Logging directly to the console can introduce additional overhead due to:
However, logging is often a common practice in JavaScript development, making this approach more convenient and widely applicable.
When it comes to measuring the cost of executing code that logs messages to the console, both approaches can introduce similar overhead due to:
Special JS feature
In the "Try Catch Throw" test case, Error
objects are used with template literals (the $
syntax). This is a modern JavaScript feature that allows for easier string interpolation. In older browsers or environments, this might require additional configuration or polyfills.
Library and its purpose
There are no libraries explicitly mentioned in the provided benchmark code. However, the use of console
implies the availability of the built-in global object in JavaScript engines.
Other alternatives
To measure similar aspects, developers can explore alternative approaches:
stacktrace-js
.Keep in mind that the choices of alternatives will depend on specific use cases, project requirements, and personal preferences.