Skip to content

Instantly share code, notes, and snippets.

@JobLeonard
Last active August 21, 2026 12:00
Show Gist options
  • Select an option

  • Save JobLeonard/810e4005c249df55818726861582a504 to your computer and use it in GitHub Desktop.

Select an option

Save JobLeonard/810e4005c249df55818726861582a504 to your computer and use it in GitHub Desktop.
Math.sumPrecise() vs Array.prototype.reduce() vs for-loop, Uint16Array

Math.sumPrecise() vs Array.prototype.reduce() vs for-loop, Uint16Array

view on jsbenchit

{"title":"Math.sumPrecise() vs Array.prototype.reduce() vs for-loop, Uint16Array","initialization":"// Obviously, assumes precision is not something to worry about\n\nconst input = new Uint16Array(10000);\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":"Array.prototype.reduce()","code":"output = input.reduce(sum);","results":{"aborted":false,"count":4450,"cycles":3,"hz":45723.47423766046,"stats":{"moe":7.86173102300982e-7,"rme":3.5946565589400548,"sem":4.011087256637663e-7,"deviation":0.000003106974829028493,"mean":0.000021870604031579538,"variance":9.653292588216634e-12,"numSamples":60},"times":{"cycle":0.09732418794052894,"elapsed":5.817,"period":0.000021870604031579538,"timeStamp":1787313575041}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:154.0) Gecko/20100101 Firefox/154.0":{"aborted":false,"count":4450,"cycles":3,"hz":45723.47423766046,"stats":{"moe":7.86173102300982e-7,"rme":3.5946565589400548,"sem":4.011087256637663e-7,"deviation":0.000003106974829028493,"mean":0.000021870604031579538,"variance":9.653292588216634e-12,"numSamples":60},"times":{"cycle":0.09732418794052894,"elapsed":5.817,"period":0.000021870604031579538,"timeStamp":1787313575041}},"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":7772.669683257919,"stats":{"moe":0.0000010352407508671636,"rme":0.8046584399138367,"sem":5.281840565648794e-7,"deviation":0.0000035035766717471156,"mean":0.00012865592399403873,"variance":1.2275049494810597e-11,"numSamples":44},"times":{"cycle":0.1255681818181818,"elapsed":5.938,"period":0.00012865592399403873,"timeStamp":1787313439797}}}},{"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":8882,"cycles":5,"hz":79614.2721555467,"stats":{"moe":0.0000011570047591186601,"rme":9.211409177773575,"sem":5.90308550570745e-7,"deviation":0.0000041321598539952145,"mean":0.000012560561981226759,"variance":1.7074745058969752e-11,"numSamples":49},"times":{"cycle":0.11156291151725607,"elapsed":6.188,"period":0.000012560561981226759,"timeStamp":1787313580863}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:154.0) Gecko/20100101 Firefox/154.0":{"aborted":false,"count":8882,"cycles":5,"hz":79614.2721555467,"stats":{"moe":0.0000011570047591186601,"rme":9.211409177773575,"sem":5.90308550570745e-7,"deviation":0.0000041321598539952145,"mean":0.000012560561981226759,"variance":1.7074745058969752e-11,"numSamples":49},"times":{"cycle":0.11156291151725607,"elapsed":6.188,"period":0.000012560561981226759,"timeStamp":1787313580863}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36":{"aborted":false,"count":16896,"cycles":5,"hz":208949.4486464713,"stats":{"moe":7.944248182453477e-8,"rme":1.6599462776343858,"sem":4.05318784819055e-8,"deviation":2.922795323147716e-7,"mean":0.000004785846559910929,"variance":8.542732501014162e-14,"numSamples":52},"times":{"cycle":0.08086166347625505,"elapsed":57.17,"period":0.000004785846559910929,"timeStamp":1787313445741}}}},{"name":"Math.sumPrecise()","code":"output = Math.sumPrecise(input);\n","results":{"aborted":false,"count":976,"cycles":2,"hz":981.0140719231628,"stats":{"moe":0.000010369649362477208,"rme":1.0172771945499195,"sem":0.000004496812386156638,"deviation":0.000013490437158469915,"mean":0.001019353369763206,"variance":1.8199189472662585e-10,"numSamples":9},"times":{"cycle":0.9948888888888889,"elapsed":9.059,"period":0.001019353369763206,"timeStamp":1787313587056}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:154.0) Gecko/20100101 Firefox/154.0":{"aborted":false,"count":976,"cycles":2,"hz":981.0140719231628,"stats":{"moe":0.000010369649362477208,"rme":1.0172771945499195,"sem":0.000004496812386156638,"deviation":0.000013490437158469915,"mean":0.001019353369763206,"variance":1.8199189472662585e-10,"numSamples":9},"times":{"cycle":0.9948888888888889,"elapsed":9.059,"period":0.001019353369763206,"timeStamp":1787313587056}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36":{"aborted":false,"count":21018,"cycles":4,"hz":266265.81096847984,"stats":{"moe":4.182592680102961e-8,"rme":1.1136814319184425,"sem":2.1339758571953882e-8,"deviation":1.5681455920641132e-7,"mean":0.000003755645519650957,"variance":2.459080597910108e-14,"numSamples":54},"times":{"cycle":0.0789361575320238,"elapsed":59.08,"period":0.000003755645519650957,"timeStamp":1787313503827}}}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment