Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.imul vs. polyfills 2
(version: 0)
Comparing performance of:
Math.imul vs Polyfill a vs Polyfill b vs Polyfill c
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var x = 0x12345678; var y; Math.imula = function(opA, opB) { opB |= 0; // ensure that opB is an integer. opA will automatically be coerced. // floating points give us 53 bits of precision to work with plus 1 sign bit // automatically handled for our convienence: // 1. 0x003fffff /*opA & 0x000fffff*/ * 0x7fffffff /*opB*/ = 0x1fffff7fc00001 // 0x1fffff7fc00001 < Number.MAX_SAFE_INTEGER /*0x1fffffffffffff*/ var result = (opA & 0x003fffff) * opB; // 2. We can remove an integer coersion from the statement above because: // 0x1fffff7fc00001 + 0xffc00000 = 0x1fffffff800001 // 0x1fffffff800001 < Number.MAX_SAFE_INTEGER /*0x1fffffffffffff*/ if (opA & 0xffc00000 /*!== 0*/) result += (opA & 0xffc00000) * opB |0; return result |0; }; Math.imulb = function(a, b) { var aHi = (a >>> 16) & 0xffff; var aLo = a & 0xffff; var bHi = (b >>> 16) & 0xffff; var bLo = b & 0xffff; // the shift by 0 fixes the sign on the high part // the final |0 converts the unsigned value into a signed value return ((aLo * bLo) + (((aHi * bLo + aLo * bHi) << 16) >>> 0) | 0); }; Math.imulc = function(x, y) { var UINT16 = 0xffff; var xn = +x; var yn = +y; var xl = UINT16 & xn; var yl = UINT16 & yn; return 0 | xl * yl + ((UINT16 & xn >>> 16) * yl + xl * (UINT16 & yn >>> 16) << 16 >>> 0); }
Tests:
Math.imul
for (var i = 0; i < 10000; ++i) { y = Math.imul(x, i); }
Polyfill a
for (var i = 0; i < 10000; ++i) { y = Math.imula(x, i); }
Polyfill b
for (var i = 0; i < 10000; ++i) { y = Math.imulb(x, i); }
Polyfill c
for (var i = 0; i < 10000; ++i) { y = Math.imulc(x, i); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Math.imul
Polyfill a
Polyfill b
Polyfill c
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of three different implementations for multiplying two integers in JavaScript: `Math.imul`, `Math.imula` (a polyfill), and `Math.imulb` (another polyfill). The tests are run on Google Chrome 105, running on Windows desktop. **Test Cases** There are four test cases: 1. **Math.imul**: Tests the built-in implementation of multiplying two integers. 2. **Polyfill a (`Math.imula`)**: Tests a custom polyfill for `Math.imul`. 3. **Polyfill b (`Math.imulb`)**: Tests another custom polyfill for `Math.imul`. 4. **Polyfill c (`Math.imulc`)**: Tests yet another custom polyfill for `Math.imul`. **Options Compared** The options compared are: * Built-in implementation (`Math.imul`) * Custom polyfill 1 (`Math.imula`) * Custom polyfill 2 (`Math.imulb`) * Custom polyfill 3 (`Math.imulc`) **Pros and Cons of Each Approach** * **Built-in implementation (`Math.imul`)**: + Pros: Efficient, well-maintained, and widely supported. + Cons: May not be optimized for specific use cases or platforms. * **Custom polyfills (1-3)**: + Pros: Can be optimized for specific use cases or platforms. + Cons: May introduce additional overhead, bugs, or compatibility issues. **Library and Purpose** None of the test cases uses a library. However, it's worth noting that `Math.imul` relies on the browser's implementation of integer multiplication, which might not be exactly what you'd implement manually. **Special JS Feature or Syntax** The benchmark doesn't use any special JavaScript features or syntax beyond the standard arithmetic operators and conditional statements. The polyfills, however, provide alternative implementations for multiplying two integers, showcasing different approaches to achieving the same result. **Other Alternatives** If you were to measure alternative implementations for multiplying two integers in JavaScript, some options could be: * Using a library like `js-integer` or `mathjs`, which provide optimized integer arithmetic functions. * Implementing a custom multiplication algorithm using bitwise operations, similar to what's done in the polyfills (`Math.imula`, `Math.imulb`, and `Math.imulc`). * Utilizing browser-specific features like WebAssembly (WASM) or SIMD instructions for parallelized arithmetic operations. Keep in mind that these alternatives might have different performance characteristics, trade-offs, or compatibility issues compared to the built-in implementation or the custom polyfills used in this benchmark.
Related benchmarks:
Math.imul vs. polyfills
Truncating a number to an integer
Number Conversion Speed
toFixed vs toPrecision vs Math.round() vs bitwise, also trunc, floor
Comments
Confirm delete:
Do you really want to delete benchmark?