Skip to content

Instantly share code, notes, and snippets.

@JobLeonard
Created September 12, 2024 14:24
Show Gist options
  • Save JobLeonard/57dab89aaac7825a8664b68dcf9ed8dc to your computer and use it in GitHub Desktop.
Save JobLeonard/57dab89aaac7825a8664b68dcf9ed8dc to your computer and use it in GitHub Desktop.
Bitmask - BigInt64 vs Uint32
{"title":"Bitmask - BigInt64 vs Uint32","initialization":"\"use strict\";\n\nclass BitArray32 {\n constructor(length) {\n this.bits = new Uint32Array(Math.ceil(length / 32));\n this.length = length;\n }\n \n and(that) {\n if (this.length !== that.length) return;\n const ret = new BitArray32(this.length);\n const {bits} = this, {bits: bits2} = that, {bits: res} = ret;\n for (let i = 0; i < bits.length; i++) {\n res[i] = bits[i] & bits2[i];\n }\n return ret;\n }\n}\n\nclass BitArray64 {\n constructor(length) {\n this.bits64 = new BigInt64Array(Math.ceil(length / 64));\n this.bits = new Uint32Array(this.bits64.buffer, 0, Math.ceil(length / 32));\n this.length = length;\n }\n \n and(that) {\n if (this.length !== that.length) return;\n const ret = new BitArray64(this.length);\n const {bits64: bits} = this, {bits64: bits2} = that, {bits64: res} = ret;\n for (let i = 0; i < bits.length; i++) {\n res[i] = bits[i] & bits2[i];\n }\n return ret;\n }\n}\n\nconst bitArr32 = [new BitArray32(10000), new BitArray32(10000)];\nconst bitArr64 = [new BitArray64(10000), new BitArray64(10000)];\nlet result = null;","setup":"const b1_32 = bitArr32[0].bits;\nconst b2_32 = bitArr32[1].bits;\nconst b1_64 = bitArr64[0].bits;\nconst b2_64 = bitArr64[1].bits;\n\nfor (let i = 0; i < b1_32.length; i++) {\n b1_32[i] = b1_64[i] = Math.random() * 0x100000000 | 0;\n b2_32[i] = b2_64[i] = Math.random() * 0x100000000 | 0;\n}","tests":[{"name":"Uint32Array masking","code":"result = bitArr32[0].and(bitArr32[1]);","results":{"aborted":false,"count":58397,"cycles":5,"hz":738660.756707153,"stats":{"moe":1.3941766015409602e-8,"rme":1.0298235434776526,"sem":7.113145926229389e-9,"deviation":5.6905167409835114e-8,"mean":0.0000013538014452776143,"variance":3.23819807794136e-15,"numSamples":64},"times":{"cycle":0.07905794299987684,"elapsed":5.953,"period":0.0000013538014452776143,"timeStamp":1726151092714}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:130.0) Gecko/20100101 Firefox/130.0":{"aborted":false,"count":73200,"cycles":3,"hz":718690.9090909091,"stats":{"moe":5.99498253022626e-8,"rme":4.308539444632429,"sem":3.058664556237888e-8,"deviation":2.2476502371357506e-7,"mean":0.0000013914187411455171,"variance":5.0519315884963966e-14,"numSamples":54},"times":{"cycle":0.10185185185185186,"elapsed":5.964,"period":0.0000013914187411455171,"timeStamp":1726151051357}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36":{"aborted":false,"count":58397,"cycles":5,"hz":738660.756707153,"stats":{"moe":1.3941766015409602e-8,"rme":1.0298235434776526,"sem":7.113145926229389e-9,"deviation":5.6905167409835114e-8,"mean":0.0000013538014452776143,"variance":3.23819807794136e-15,"numSamples":64},"times":{"cycle":0.07905794299987684,"elapsed":5.953,"period":0.0000013538014452776143,"timeStamp":1726151092714}}}},{"name":"BigInt64Array masking","code":"result = bitArr64[0].and(bitArr64[1]);","results":{"aborted":false,"count":75006,"cycles":7,"hz":911757.7825365602,"stats":{"moe":2.1150250812078728e-8,"rme":1.9283905780512982,"sem":1.0790944291876902e-8,"deviation":8.565046502357483e-8,"mean":0.000001096782521798657,"variance":7.336002158754615e-15,"numSamples":63},"times":{"cycle":0.08226526983003007,"elapsed":6.152,"period":0.000001096782521798657,"timeStamp":1726151098672}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:130.0) Gecko/20100101 Firefox/130.0":{"aborted":false,"count":23645,"cycles":3,"hz":249412.3852936407,"stats":{"moe":1.522826337908495e-7,"rme":3.798117493257374,"sem":7.769522132186199e-8,"deviation":5.762031828217556e-7,"mean":0.0000040094239859927965,"variance":3.3201010789392143e-13,"numSamples":55},"times":{"cycle":0.09480283014879967,"elapsed":5.804,"period":0.0000040094239859927965,"timeStamp":1726151057326}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36":{"aborted":false,"count":75006,"cycles":7,"hz":911757.7825365602,"stats":{"moe":2.1150250812078728e-8,"rme":1.9283905780512982,"sem":1.0790944291876902e-8,"deviation":8.565046502357483e-8,"mean":0.000001096782521798657,"variance":7.336002158754615e-15,"numSamples":63},"times":{"cycle":0.08226526983003007,"elapsed":6.152,"period":0.000001096782521798657,"timeStamp":1726151098672}}}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment