Last active
August 21, 2026 11:52
-
-
Save JobLeonard/abf2aed468c5cc4ab33eda3d02fe0c38 to your computer and use it in GitHub Desktop.
Math.sumPrecise() vs Array.prototype.reduce() vs for-loop, SMI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {"title":"Math.sumPrecise() vs Array.prototype.reduce() vs for-loop, SMI","initialization":"// Obviously, assumes precision is not something to worry about\n\nconst input = [];\nfor (let i = 0; i < 10000; i++) {\n input[i] = 0x10000 * Math.random() | 0;\n}\n// ensures the calculation isn't optimized away\nlet output = 0;\n\nfunction sum(accumulator, currentValue) {\n return accumulator + currentValue;\n}","setup":"for (let i = 0, {random} = Math; i < input.length; i++) {\n input[i] = 0x10000 * random() | 0;\n}","tests":[{"name":"for-loop","code":"let acc = 0;\nfor (let i = 0; i < input.length; i++) {\n acc += input[i];\n}\noutput = acc;","results":{"aborted":false,"count":14081,"cycles":6,"hz":178197.38968417922,"stats":{"moe":1.3561467187585228e-7,"rme":2.416618053115335,"sem":6.919115912033279e-8,"deviation":5.535292729626623e-7,"mean":0.000005611754480648166,"variance":3.063946560265735e-13,"numSamples":64},"times":{"cycle":0.07901911484200683,"elapsed":5.932,"period":0.000005611754480648166,"timeStamp":1787313044900}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:154.0) Gecko/20100101 Firefox/154.0":{"aborted":false,"count":5396,"cycles":5,"hz":59681.74328489518,"stats":{"moe":9.931170089650184e-7,"rme":5.9270954380913174,"sem":5.066923515127645e-7,"deviation":0.00000382544336353938,"mean":0.000016755542733167606,"variance":1.463401692764749e-11,"numSamples":57},"times":{"cycle":0.0904129085881724,"elapsed":6.022,"period":0.000016755542733167606,"timeStamp":1787312793160}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36":{"aborted":false,"count":14081,"cycles":6,"hz":178197.38968417922,"stats":{"moe":1.3561467187585228e-7,"rme":2.416618053115335,"sem":6.919115912033279e-8,"deviation":5.535292729626623e-7,"mean":0.000005611754480648166,"variance":3.063946560265735e-13,"numSamples":64},"times":{"cycle":0.07901911484200683,"elapsed":5.932,"period":0.000005611754480648166,"timeStamp":1787313044900}}}},{"name":"Array.prototype.reduce()","code":"output = input.reduce(sum);","results":{"aborted":false,"count":976,"cycles":2,"hz":9258.928571428572,"stats":{"moe":0.0000030428154593248144,"rme":2.817321099392708,"sem":0.0000015524568670024563,"deviation":0.000011086759604097908,"mean":0.00010800385728061716,"variance":1.229162385190572e-10,"numSamples":51},"times":{"cycle":0.10541176470588234,"elapsed":52.098,"period":0.00010800385728061716,"timeStamp":1787313050838}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:154.0) Gecko/20100101 Firefox/154.0":{"aborted":false,"count":4493,"cycles":7,"hz":57399.90261426158,"stats":{"moe":9.386851347473789e-8,"rme":0.5388043531995456,"sem":4.789209871160096e-8,"deviation":3.8907724921166264e-7,"mean":0.000017421632345270565,"variance":1.5138110585411422e-13,"numSamples":66},"times":{"cycle":0.07827539412730065,"elapsed":6.16,"period":0.000017421632345270565,"timeStamp":1787312799188}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36":{"aborted":false,"count":976,"cycles":2,"hz":9258.928571428572,"stats":{"moe":0.0000030428154593248144,"rme":2.817321099392708,"sem":0.0000015524568670024563,"deviation":0.000011086759604097908,"mean":0.00010800385728061716,"variance":1.229162385190572e-10,"numSamples":51},"times":{"cycle":0.10541176470588234,"elapsed":52.098,"period":0.00010800385728061716,"timeStamp":1787313050838}}}},{"name":"Math.sumPrecise()","code":"output = Math.sumPrecise(input);\n","results":{"aborted":false,"count":13242,"cycles":4,"hz":169105.56527041033,"stats":{"moe":5.4127160776663426e-8,"rme":0.9153204119620052,"sem":2.7615898355440523e-8,"deviation":2.029345792781832e-7,"mean":0.0000059134659370963794,"variance":4.118244346681322e-14,"numSamples":54},"times":{"cycle":0.07830611593903025,"elapsed":58.081,"period":0.0000059134659370963794,"timeStamp":1787313103827}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:154.0) Gecko/20100101 Firefox/154.0":{"aborted":false,"count":976,"cycles":2,"hz":6000.351308624628,"stats":{"moe":0.0000016402564964288902,"rme":0.9842115214827137,"sem":8.36865559402495e-7,"deviation":0.000004950963417153451,"mean":0.00016665690866510537,"variance":2.4512038757991777e-11,"numSamples":35},"times":{"cycle":0.16265714285714283,"elapsed":5.987,"period":0.00016665690866510537,"timeStamp":1787312805354}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36":{"aborted":false,"count":13242,"cycles":4,"hz":169105.56527041033,"stats":{"moe":5.4127160776663426e-8,"rme":0.9153204119620052,"sem":2.7615898355440523e-8,"deviation":2.029345792781832e-7,"mean":0.0000059134659370963794,"variance":4.118244346681322e-14,"numSamples":54},"times":{"cycle":0.07830611593903025,"elapsed":58.081,"period":0.0000059134659370963794,"timeStamp":1787313103827}}}}]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment