Skip to content

Instantly share code, notes, and snippets.

@JobLeonard
Last active March 30, 2023 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JobLeonard/11b875593306fe1ccdba553e5be14e3f to your computer and use it in GitHub Desktop.
Save JobLeonard/11b875593306fe1ccdba553e5be14e3f to your computer and use it in GitHub Desktop.
Bit Array iterator test bail early forEach
{"title":"Bit Array iterator test bail early forEach","initialization":"class BitArray1 {\n constructor(length) {\n this.bits = new Uint32Array(Math.ceil(length / 32));\n this.length = length;\n }\n\n forEach(cb) {\n const {bits, length} = this;\n let i = 0, v = 0;\n while(i + 32 < length) {\n v = bits[i >> 5];\n for (let j = 1; j; j <<= 1) {\n\t cb((v & j) ? true : false, i++, bits);\n }\n }\n v = bits[i >> 5];\n for(; i < length; i++) {\n cb((v & 1) ? true : false, i, bits);\n v >>= 1;\n }\n }\n}\n\nclass BitArray2 {\n constructor(length) {\n this.bits = new Uint32Array(Math.ceil(length / 32));\n this.length = length;\n }\n\n forEach(cb) {\n const {bits, length} = this;\n let i = 0;\n while(i + 32 < length) {\n const v = bits[i >> 5];\n if (v) {\n for (let j = 0; j < 32; j++) cb((v & (1 << j)) ? true : false, i++, bits);\n } else {\n for (let j = 0; j < 32; j++) cb(false, i++, bits);\n }\n }\n let v = bits[i >> 5];\n while(i < length) {\n cb((v & 1) ? true : false, i++, bits);\n v >>>= 1;\n }\n }\n}\n\nconst bitArr1 = new BitArray1(10000);\nconst bitArr2 = new BitArray2(10000);","setup":"\n{\n const {random} = Math;\n for (let i = 0, {bits} = bitArr1; i < bits.length; i++) {\n bits[i] =\n random() * 0x100000000 &\n random() * 0x100000000 &\n random() * 0x100000000 &\n random() * 0x100000000 &\n random() * 0x100000000 &\n random() * 0x100000000 &\n random() * 0x100000000 &\n random() * 0x100000000 &\n random() * 0x100000000 &\n random() * 0x100000000;\n }\n bitArr2.bits.set(bitArr1.bits);\n}","tests":[{"name":"forEach no bail","code":"const t = [false];\nbitArr1.forEach((v, i) => t[i] = v);","results":{"aborted":false,"count":976,"cycles":2,"hz":4074.135915845716,"stats":{"moe":0.000011905445780682232,"rme":4.850440424923132,"sem":0.0000057681423356018565,"deviation":0.00002884071167800928,"mean":0.0002454508196721312,"variance":8.317866500940609e-10,"numSamples":25},"times":{"cycle":0.23956000000000005,"elapsed":6.278,"period":0.0002454508196721312,"timeStamp":1680183368710}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36":{"aborted":false,"count":589,"cycles":6,"hz":7636.6020686204065,"stats":{"moe":0.0000015178429026178626,"rme":1.159116224997237,"sem":7.74409644192787e-7,"deviation":0.000006291333690342672,"mean":0.00013094829231826864,"variance":3.958087960324074e-11,"numSamples":66},"times":{"cycle":0.07712854417546022,"elapsed":6.406,"period":0.00013094829231826864,"timeStamp":1680183327252}},"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0":{"aborted":false,"count":976,"cycles":2,"hz":4074.135915845716,"stats":{"moe":0.000011905445780682232,"rme":4.850440424923132,"sem":0.0000057681423356018565,"deviation":0.00002884071167800928,"mean":0.0002454508196721312,"variance":8.317866500940609e-10,"numSamples":25},"times":{"cycle":0.23956000000000005,"elapsed":6.278,"period":0.0002454508196721312,"timeStamp":1680183368710}}}},{"name":"forEach with bail","code":"const t = [false];\nbitArr2.forEach((v, i) => t[i] = v);\n","results":{"aborted":false,"count":346,"cycles":4,"hz":4000.978886680303,"stats":{"moe":0.000007311674068576278,"rme":2.9253853574661557,"sem":0.000003730445953355244,"deviation":0.00002889591010254342,"mean":0.0002499388345509919,"variance":8.34973620654271e-10,"numSamples":60},"times":{"cycle":0.0864788367546432,"elapsed":5.843,"period":0.0002499388345509919,"timeStamp":1680183374993}},"platforms":{"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0":{"aborted":false,"count":346,"cycles":4,"hz":4000.978886680303,"stats":{"moe":0.000007311674068576278,"rme":2.9253853574661557,"sem":0.000003730445953355244,"deviation":0.00002889591010254342,"mean":0.0002499388345509919,"variance":8.34973620654271e-10,"numSamples":60},"times":{"cycle":0.0864788367546432,"elapsed":5.843,"period":0.0002499388345509919,"timeStamp":1680183374993}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36":{"aborted":false,"count":623,"cycles":4,"hz":8184.368946054271,"stats":{"moe":5.731709089070634e-7,"rme":0.46910421876406716,"sem":2.9243413719748134e-7,"deviation":0.0000024114736724031643,"mean":0.00012218413008886964,"variance":5.815205272693604e-12,"numSamples":68},"times":{"cycle":0.07612071304536579,"elapsed":6.262,"period":0.00012218413008886964,"timeStamp":1680183333668}}}},{"name":"inlined \"simple\" for-loop","code":"const t = [false], {bits, length} = bitArr1;\nfor (let i = 0; i < length; i++) t[i] = (bits[i >> 5] & (1 << (i & 31))) ? true : false;","results":{"aborted":false,"count":976,"cycles":2,"hz":6940.444444444443,"stats":{"moe":0.000007201163195757641,"rme":4.997927309553392,"sem":0.000003674062854978388,"deviation":0.000023236813776705228,"mean":0.0001440829918032787,"variance":5.399495144932779e-10,"numSamples":40},"times":{"cycle":0.14062500000000003,"elapsed":5.977,"period":0.0001440829918032787,"timeStamp":1680183380841}},"platforms":{"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0":{"aborted":false,"count":976,"cycles":2,"hz":6940.444444444443,"stats":{"moe":0.000007201163195757641,"rme":4.997927309553392,"sem":0.000003674062854978388,"deviation":0.000023236813776705228,"mean":0.0001440829918032787,"variance":5.399495144932779e-10,"numSamples":40},"times":{"cycle":0.14062500000000003,"elapsed":5.977,"period":0.0001440829918032787,"timeStamp":1680183380841}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36":{"aborted":false,"count":976,"cycles":2,"hz":6914.441416893734,"stats":{"moe":8.331395217697706e-7,"rme":0.5760694415375941,"sem":4.250711845764136e-7,"deviation":0.000002654568696856539,"mean":0.000144624842370744,"variance":7.046734966330625e-12,"numSamples":39},"times":{"cycle":0.14115384615384616,"elapsed":5.96,"period":0.000144624842370744,"timeStamp":1680183339940}}}},{"name":"in-lined for-loop avoid lookup + bail","code":"const t = [false], {bits, length} = bitArr1;\nlet i = 0;\nwhile(i + 32 < length) {\n const v = bits[i >> 5];\n if (v) {\n for (let j = 1; j; j <<= 1) {\n t[i++] = (v & j) ? true : false;\n }\n } else {\n for (let j = 0; j < 32; j++) t[i++] = false;\n } \n}\nlet v = bits[i >> 5];\nwhile(i < length) {\n t[i++] = (v & 1) ? true : false;\n v >>>= 1;\n}","results":{"aborted":false,"count":976,"cycles":2,"hz":7015.112421673423,"stats":{"moe":0.000007661802822052467,"rme":5.374840814939275,"sem":0.000003909083072475749,"deviation":0.000024412215963184113,"mean":0.0001425493905002102,"variance":5.959562882331413e-10,"numSamples":39},"times":{"cycle":0.13912820512820515,"elapsed":5.77,"period":0.0001425493905002102,"timeStamp":1680183386822}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36":{"aborted":false,"count":1592,"cycles":2,"hz":6752.937964710941,"stats":{"moe":0.0000015918596820672182,"rme":1.0749729681524407,"sem":7.742508181260789e-7,"deviation":0.000004023125264388417,"mean":0.0001480836941233185,"variance":1.6185536892960367e-11,"numSamples":27},"times":{"cycle":0.23574924104432304,"elapsed":5.984,"period":0.0001480836941233185,"timeStamp":1680183345908}},"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0":{"aborted":false,"count":976,"cycles":2,"hz":7015.112421673423,"stats":{"moe":0.000007661802822052467,"rme":5.374840814939275,"sem":0.000003909083072475749,"deviation":0.000024412215963184113,"mean":0.0001425493905002102,"variance":5.959562882331413e-10,"numSamples":39},"times":{"cycle":0.13912820512820515,"elapsed":5.77,"period":0.0001425493905002102,"timeStamp":1680183386822}}}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment