Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.imul vs. polyfills
(version: 0)
Comparing performance of:
Math.imul vs Polyfill a vs Polyfill b
Created:
5 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); };
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); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math.imul
Polyfill a
Polyfill b
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.imul
554.3 Ops/sec
Polyfill a
544.0 Ops/sec
Polyfill b
554.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmarking test case on the MeasureThat.net website. The goal is to compare the performance of three different implementations of the `Math.imul` function: the original implementation, and two polyfills (polyfill "a" and polyfill "b"). **Original Implementation** The original implementation is not provided in the JSON data, but it's likely the same as the one used in modern JavaScript engines. The implementation uses a combination of bitwise operations to perform the multiplication. ```javascript Math.imul = function(opA, opB) { // ... } ``` The pros of this implementation are: * It's highly optimized for performance and uses low-level bitwise operations. * It's likely to be well-optimized by modern JavaScript engines. However, it may have some cons: * It might not work correctly for all edge cases or invalid inputs. * It may rely on internal engine implementation details that could change in the future. **Polyfill "a"** The polyfill "a" is implemented as follows: ```javascript Math.imul = function(opA, opB) { // ... } ``` This implementation seems to be a direct copy of the original implementation, without any significant changes. The pros are: * It's likely to work correctly for all edge cases and invalid inputs. * It provides a fallback for browsers that don't support the `Math.imul` function. However, it might have some cons: * It may not be optimized for performance, as it doesn't use low-level bitwise operations like the original implementation. * It relies on the internal engine implementation details of MeasureThat.net, which could change in the future. **Polyfill "b"** The polyfill "b" is implemented as follows: ```javascript Math.imulb = function(a, b) { // ... } ``` This implementation seems to use a different approach to perform the multiplication. The pros are: * It might be optimized for performance, using low-level bitwise operations. * It provides an alternative implementation that can work in different browsers. However, it might have some cons: * It's not directly comparable to the original implementation, as it uses a different function name and signature. * It relies on the internal engine implementation details of MeasureThat.net, which could change in the future. **Other Considerations** In addition to the above implementations, there may be other polyfills or alternatives available for `Math.imul`. Some examples include: * Using a library like Polyfill.io or js-polyfill that provides a set of polyfills for various JavaScript functions. * Implementing a custom polyfill using a different approach, such as using a library like Bitwise.js. **Device Platform and Browser** The benchmark results show the performance of each implementation on a desktop browser (Chrome 128) with Windows. The pros of this are: * It provides a clear and consistent environment for testing the implementations. * It allows for accurate comparisons between the different implementations. However, it might have some cons: * The results may not be representative of other devices or browsers. * The performance differences between the implementations may be exaggerated by the specific browser and device platform used.
Related benchmarks:
Truncating a number to an integer
Number Conversion Speed
Math.imul vs. polyfills 2
toFixed vs toPrecision vs Math.round() vs bitwise, also trunc, floor
Comments
Confirm delete:
Do you really want to delete benchmark?