var values = [];
const rand = () => Math.floor(5+Math.random()*10);
for(let i=0; i<50; i++){
values.push([rand(), rand()]);
}
values.forEach(x => (Math.pow(x[0], x[1])))
values.forEach(x => (x[0] ** x[1]))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
pow | |
** |
Test name | Executions per second |
---|---|
pow | 112927.5 Ops/sec |
** | 5589173.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is comparing the performance of two ways to calculate the power of a number: using the Math.pow()
function versus exponentiation operator (**
).
Options Compared
There are only two options compared in this benchmark:
Math.pow()
: This method takes two arguments, the base and the exponent, and returns the result.**
): This is a new feature introduced in ECMAScript 2016 (ES6) that allows for exponentiation without explicitly calling a function.Pros and Cons
Using Math.pow()
has some pros:
However, it also has some cons:
**
operator.The exponentiation operator (**
) has its own pros:
However, it also has some cons:
x**y
instead of x.pow(y)
).Library and Special JS Feature
There is no library being used in this benchmark. The Math.pow()
function is a built-in part of the JavaScript standard library.
The exponentiation operator (**
) is also a built-in feature, but it was introduced as a new syntax in ES6.
Other Considerations
Other Alternatives
If you wanted to create a similar benchmark, you could consider adding more options or variants, such as:
Math.sqrt()
instead of Math.pow()