Last active
September 23, 2026 13:07
-
-
Save JobLeonard/58e06e2ccac3b7b2d0f884d962ee777f to your computer and use it in GitHub Desktop.
Truncating to 32-bit integer values in JavaScript
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":"Truncating to 32-bit integer values in JavaScript","initialization":"// Ensures that the loop isn't optimized away,\n// also represents somewhat realistic overhead\nconst t = [0], s = [-0.0];\n\nfunction trunc() {\n for (let i = 0; i < s.length; i++) {\n t[i] = Math.trunc(s[i]);\n }\n}\n\nfunction notnot(){\n for (let i = 0; i < s.length; i++) {\n t[i] = ~~s[i];\n }\n}\n\nfunction orzero(){\n for (let i = 0; i < s.length; i++) {\n t[i] = s[i] | 0;\n }\n}","setup":"for (let i = 0; i < 10000; i++) {\n s[i] = Math.random() * 256;\n t[i] = 0;\n}","tests":[{"name":"Math.trunc(v)","code":"trunc();","results":{"aborted":false,"count":2483,"cycles":3,"hz":24066.81498601105,"stats":{"moe":0.0000025691761450007256,"rme":6.183188694820556,"sem":0.000001310804155612615,"deviation":0.000008595517667829316,"mean":0.0000415509904647231,"variance":7.388292397796594e-11,"numSamples":43},"times":{"cycle":0.10317110932390745,"elapsed":45.739,"period":0.0000415509904647231,"timeStamp":1790167530189}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:156.0) Gecko/20100101 Firefox/156.0":{"aborted":false,"count":1945,"cycles":3,"hz":22809.210051332146,"stats":{"moe":6.150125706209791e-7,"rme":1.402795090750366,"sem":3.1378192378621385e-7,"deviation":0.0000024305443303298625,"mean":0.00004384193918813932,"variance":5.9075457416986405e-12,"numSamples":60},"times":{"cycle":0.08527257172093097,"elapsed":5.927,"period":0.00004384193918813932,"timeStamp":1790167348932}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/153.0.0.0 Safari/537.36":{"aborted":false,"count":2483,"cycles":3,"hz":24066.81498601105,"stats":{"moe":0.0000025691761450007256,"rme":6.183188694820556,"sem":0.000001310804155612615,"deviation":0.000008595517667829316,"mean":0.0000415509904647231,"variance":7.388292397796594e-11,"numSamples":43},"times":{"cycle":0.10317110932390745,"elapsed":45.739,"period":0.0000415509904647231,"timeStamp":1790167530189}}}},{"name":"double-NOT","code":"notnot();","results":{"aborted":false,"count":4122,"cycles":3,"hz":46533.79023178735,"stats":{"moe":7.358229238451533e-7,"rme":3.4240629585950804,"sem":3.7541985910467006e-7,"deviation":0.0000026279390137326905,"mean":0.0000214897603444496,"variance":6.906063459898346e-12,"numSamples":49},"times":{"cycle":0.08858079213982126,"elapsed":52.085,"period":0.0000214897603444496,"timeStamp":1790167576818}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:156.0) Gecko/20100101 Firefox/156.0":{"aborted":false,"count":2193,"cycles":3,"hz":25899.97291150975,"stats":{"moe":6.004516488909321e-7,"rme":1.5551681440946505,"sem":3.0635288208721026e-7,"deviation":0.00000237299922077282,"mean":0.00003861007899184356,"variance":5.63112530178841e-12,"numSamples":60},"times":{"cycle":0.08467190322911293,"elapsed":6.021,"period":0.00003861007899184356,"timeStamp":1790167354865}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/153.0.0.0 Safari/537.36":{"aborted":false,"count":4122,"cycles":3,"hz":46533.79023178735,"stats":{"moe":7.358229238451533e-7,"rme":3.4240629585950804,"sem":3.7541985910467006e-7,"deviation":0.0000026279390137326905,"mean":0.0000214897603444496,"variance":6.906063459898346e-12,"numSamples":49},"times":{"cycle":0.08858079213982126,"elapsed":52.085,"period":0.0000214897603444496,"timeStamp":1790167576818}}}},{"name":"OR-zero bitmask","code":"orzero();","results":{"aborted":false,"count":4693,"cycles":4,"hz":30835.553933199488,"stats":{"moe":0.0000011005640166288947,"rme":3.393650109169894,"sem":5.615122533820892e-7,"deviation":0.0000033690735202925353,"mean":0.00003243009683452897,"variance":1.1350656385136336e-11,"numSamples":36},"times":{"cycle":0.15219444444444444,"elapsed":39.145,"period":0.00003243009683452897,"timeStamp":1790167629818}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/153.0.0.0 Safari/537.36":{"aborted":false,"count":4693,"cycles":4,"hz":30835.553933199488,"stats":{"moe":0.0000011005640166288947,"rme":3.393650109169894,"sem":5.615122533820892e-7,"deviation":0.0000033690735202925353,"mean":0.00003243009683452897,"variance":1.1350656385136336e-11,"numSamples":36},"times":{"cycle":0.15219444444444444,"elapsed":39.145,"period":0.00003243009683452897,"timeStamp":1790167629818}},"Mozilla/5.0 (X11; Linux x86_64; rv:156.0) Gecko/20100101 Firefox/156.0":{"aborted":false,"count":3178,"cycles":3,"hz":32535.2673354135,"stats":{"moe":0.0000010279127715447728,"rme":3.344341681969501,"sem":5.244452916044759e-7,"deviation":0.000003889390378161558,"mean":0.000030735877768907557,"variance":1.5127357513735706e-11,"numSamples":55},"times":{"cycle":0.09767861954958822,"elapsed":5.899,"period":0.000030735877768907557,"timeStamp":1790167360892}}}},{"name":"Math.trunc(v) round two","code":"trunc();","results":{"aborted":false,"count":2712,"cycles":3,"hz":21739.47895791583,"stats":{"moe":0.0000027407668078453406,"rme":5.958284234770792,"sem":0.0000013983504121659902,"deviation":0.000009275607285186829,"mean":0.00004599926253687316,"variance":8.603689050901098e-11,"numSamples":44},"times":{"cycle":0.12475000000000001,"elapsed":46.14,"period":0.00004599926253687316,"timeStamp":1790167669818}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:156.0) Gecko/20100101 Firefox/156.0":{"aborted":false,"count":1890,"cycles":5,"hz":23071.776250011022,"stats":{"moe":4.49055522141017e-7,"rme":1.0360508530669414,"sem":2.291099602760291e-7,"deviation":0.0000018040136312279863,"mean":0.00004334300008650276,"variance":3.254465181656385e-12,"numSamples":62},"times":{"cycle":0.08191827016349022,"elapsed":6.064,"period":0.00004334300008650276,"timeStamp":1790167366796}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/153.0.0.0 Safari/537.36":{"aborted":false,"count":2712,"cycles":3,"hz":21739.47895791583,"stats":{"moe":0.0000027407668078453406,"rme":5.958284234770792,"sem":0.0000013983504121659902,"deviation":0.000009275607285186829,"mean":0.00004599926253687316,"variance":8.603689050901098e-11,"numSamples":44},"times":{"cycle":0.12475000000000001,"elapsed":46.14,"period":0.00004599926253687316,"timeStamp":1790167669818}}}},{"name":"double-NOT round two","code":"notnot();","results":{"aborted":false,"count":4880,"cycles":3,"hz":44229.60725075529,"stats":{"moe":4.4316931289065875e-7,"rme":1.9601204654740918,"sem":2.2610679229115242e-7,"deviation":0.0000015665138087387958,"mean":0.000022609289617486338,"variance":2.4539655129693286e-12,"numSamples":48},"times":{"cycle":0.11033333333333332,"elapsed":50.122,"period":0.000022609289617486338,"timeStamp":1790167716818}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:156.0) Gecko/20100101 Firefox/156.0":{"aborted":false,"count":2980,"cycles":4,"hz":35697.1875862155,"stats":{"moe":3.1701957280983983e-7,"rme":1.1316707159094754,"sem":1.6174468000502034e-7,"deviation":0.000001273578883939051,"mean":0.00002801341135305995,"variance":1.6220031736154388e-12,"numSamples":62},"times":{"cycle":0.08347996583211865,"elapsed":5.91,"period":0.00002801341135305995,"timeStamp":1790167372865}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/153.0.0.0 Safari/537.36":{"aborted":false,"count":4880,"cycles":3,"hz":44229.60725075529,"stats":{"moe":4.4316931289065875e-7,"rme":1.9601204654740918,"sem":2.2610679229115242e-7,"deviation":0.0000015665138087387958,"mean":0.000022609289617486338,"variance":2.4539655129693286e-12,"numSamples":48},"times":{"cycle":0.11033333333333332,"elapsed":50.122,"period":0.000022609289617486338,"timeStamp":1790167716818}}}},{"name":"OR-zero bitmask round t wo","code":"orzero();","results":{"aborted":false,"count":3691,"cycles":3,"hz":30888.667837655154,"stats":{"moe":0.0000015751633218707389,"rme":4.8654696639322745,"sem":8.036547560564994e-7,"deviation":0.0000048884410374364305,"mean":0.00003237433240098945,"variance":2.3896855776492568e-11,"numSamples":37},"times":{"cycle":0.11949366089205206,"elapsed":40.136,"period":0.00003237433240098945,"timeStamp":1790167767818}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:156.0) Gecko/20100101 Firefox/156.0":{"aborted":false,"count":2964,"cycles":8,"hz":35570.92972655519,"stats":{"moe":3.93271003086382e-7,"rme":1.3989015214277563,"sem":2.006484709624398e-7,"deviation":0.0000015799076402666812,"mean":0.000028112844046734548,"variance":2.4961081517730327e-12,"numSamples":62},"times":{"cycle":0.0833264697545212,"elapsed":6.197,"period":0.000028112844046734548,"timeStamp":1790167378780}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/153.0.0.0 Safari/537.36":{"aborted":false,"count":3691,"cycles":3,"hz":30888.667837655154,"stats":{"moe":0.0000015751633218707389,"rme":4.8654696639322745,"sem":8.036547560564994e-7,"deviation":0.0000048884410374364305,"mean":0.00003237433240098945,"variance":2.3896855776492568e-11,"numSamples":37},"times":{"cycle":0.11949366089205206,"elapsed":40.136,"period":0.00003237433240098945,"timeStamp":1790167767818}}}}]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment