Skip to content

Instantly share code, notes, and snippets.

@JobLeonard
Last active February 26, 2024 10:49
Show Gist options
  • Save JobLeonard/e4ac3cb90b4a395dd7a873fd51267d48 to your computer and use it in GitHub Desktop.
Save JobLeonard/e4ac3cb90b4a395dd7a873fd51267d48 to your computer and use it in GitHub Desktop.
Verifying alexharri.com/blog/bit-set-iteration benchmarks - empty TypedArray

Verifying alexharri.com/blog/bit-set-iteration benchmarks - empty TypedArray

view on jsbenchit

{"title":"Verifying alexharri.com/blog/bit-set-iteration benchmarks - empty TypedArray","initialization":"// Veryfying the nrs in https://alexharri.com/blog/bit-set-iteration\nconst WORD_LEN = 32;\nconst WORD_LOG = Math.log2(WORD_LEN);\n\n// Typed Array Versions\nclass BitSetBasicTA {\n constructor(size) {\n this.words = new Uint32Array(size);\n }\n \n forEach(callback) {\n const words = this.words;\n\n for (let wordIndex = 0; wordIndex < words.length; wordIndex++) {\n const word = words[wordIndex];\n\n for (let i = 0; i < WORD_LEN; i++) {\n const bitIsSetToOne = (word & (1 << i)) !== 0;\n if (bitIsSetToOne) {\n const index = (wordIndex << WORD_LOG) + i;\n callback(index);\n }\n }\n }\n }\n}\n\n// Returns the number of non-zero bits in `n`\nfunction hammingWeight(n) {\n n -= (n >> 1) & 0x55555555;\n n = (n & 0x33333333) + ((n >>> 2) & 0x33333333);\n return (((n + (n >>> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;\n}\n\nclass BitSetOptimizedTA {\n constructor(size) {\n this.words = new Uint32Array(size);\n }\n \n forEach(callback) {\n const words = this.words;\n for (let wordIndex = 0; wordIndex < words.length; wordIndex++) {\n let word = words[wordIndex];\n while (word !== 0) {\n const lsb = word & -word;\n const index = (wordIndex << WORD_LOG) + hammingWeight(lsb - 1);\n callback(index);\n word ^= lsb;\n }\n }\n }\n}\n\n\nclass BitSetCLZ_TA {\n constructor(size) {\n this.words = new Uint32Array(size);\n }\n \n forEach(callback) {\n const words = this.words;\n const clz32 = Math.clz32;\n for (let wordIndex = 0; wordIndex < words.length; wordIndex++) {\n let word = words[wordIndex];\n while (word !== 0) {\n const index = 31 - clz32(word);\n callback(index);\n word ^= (1 << index);\n }\n }\n }\n}\n\nconst size = 10000;\nconst basic = new BitSetBasicTA(size);\nconst optimized = new BitSetOptimizedTA(size);\nconst clz = new BitSetCLZ_TA(size);","setup":"// runs before each test","tests":[{"name":"basic.forEach(() => {})","code":"basic.forEach(() => {});","results":{"aborted":false,"count":976,"cycles":2,"hz":2609.2380810931227,"stats":{"moe":0.00000652199095873693,"rme":1.701742717408144,"sem":0.000003090990975704706,"deviation":0.000013113964077043323,"mean":0.00038325364298724966,"variance":1.7197605381398276e-10,"numSamples":18},"times":{"cycle":0.3740555555555557,"elapsed":6.95,"period":0.00038325364298724966,"timeStamp":1708944530189}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":976,"cycles":2,"hz":2609.2380810931227,"stats":{"moe":0.00000652199095873693,"rme":1.701742717408144,"sem":0.000003090990975704706,"deviation":0.000013113964077043323,"mean":0.00038325364298724966,"variance":1.7197605381398276e-10,"numSamples":18},"times":{"cycle":0.3740555555555557,"elapsed":6.95,"period":0.00038325364298724966,"timeStamp":1708944530189}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":249,"cycles":4,"hz":3211.5193644488572,"stats":{"moe":0.0000014996366361146757,"rme":0.4816112096519225,"sem":7.651207327115692e-7,"deviation":0.0000062158702167319945,"mean":0.0003113790970933829,"variance":3.8637042551255856e-11,"numSamples":66},"times":{"cycle":0.07753339517625234,"elapsed":5.823,"period":0.0003113790970933829,"timeStamp":1708944471348}}}},{"name":"optimized.forEach(() => {})","code":"optimized.forEach(() => {});","results":{"aborted":false,"count":5625,"cycles":3,"hz":58966.679146387134,"stats":{"moe":5.797829138224875e-7,"rme":3.4187873053928044,"sem":2.9580760909310587e-7,"deviation":0.000002213621451254316,"mean":0.000016958730158730155,"variance":4.900119929453264e-12,"numSamples":56},"times":{"cycle":0.09539285714285713,"elapsed":5.809,"period":0.000016958730158730155,"timeStamp":1708944537145}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":5625,"cycles":3,"hz":58966.679146387134,"stats":{"moe":5.797829138224875e-7,"rme":3.4187873053928044,"sem":2.9580760909310587e-7,"deviation":0.000002213621451254316,"mean":0.000016958730158730155,"variance":4.900119929453264e-12,"numSamples":56},"times":{"cycle":0.09539285714285713,"elapsed":5.809,"period":0.000016958730158730155,"timeStamp":1708944537145}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":3328,"cycles":3,"hz":40524.54102660174,"stats":{"moe":2.651208226149533e-7,"rme":1.0743899653066078,"sem":1.3526572582395574e-7,"deviation":0.0000010905471461034144,"mean":0.000024676405325443774,"variance":1.1892930778743018e-12,"numSamples":65},"times":{"cycle":0.08212307692307688,"elapsed":5.836,"period":0.000024676405325443774,"timeStamp":1708944477176}}}},{"name":"clz.forEach(() => {})","code":"clz.forEach(() => {});","results":{"aborted":false,"count":5546,"cycles":7,"hz":59544.54699566773,"stats":{"moe":5.178984415152278e-7,"rme":3.083802808978656,"sem":2.6423389873225907e-7,"deviation":0.0000019949221876146536,"mean":0.000016794149094336997,"variance":3.979714534637235e-12,"numSamples":57},"times":{"cycle":0.09314035087719298,"elapsed":6.08,"period":0.000016794149094336997,"timeStamp":1708944542961}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":5546,"cycles":7,"hz":59544.54699566773,"stats":{"moe":5.178984415152278e-7,"rme":3.083802808978656,"sem":2.6423389873225907e-7,"deviation":0.0000019949221876146536,"mean":0.000016794149094336997,"variance":3.979714534637235e-12,"numSamples":57},"times":{"cycle":0.09314035087719298,"elapsed":6.08,"period":0.000016794149094336997,"timeStamp":1708944542961}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":3486,"cycles":3,"hz":41689.0660592255,"stats":{"moe":1.3001313645907828e-7,"rme":0.5420126234409615,"sem":6.633323288728484e-8,"deviation":5.265037136360601e-7,"mean":0.000023987104882113495,"variance":2.772061604725624e-13,"numSamples":63},"times":{"cycle":0.08361904761904765,"elapsed":5.744,"period":0.000023987104882113495,"timeStamp":1708944483017}}}},{"name":"basic.forEach(value => sum += value)","code":"let sum = 0;\nbasic.forEach(value => sum += value);","results":{"aborted":false,"count":976,"cycles":2,"hz":2615.4533273782936,"stats":{"moe":0.000002925901710695216,"rme":0.7652559364819644,"sem":0.0000013866832752110029,"deviation":0.00000588319888355803,"mean":0.0003823428961748634,"variance":3.4612029103498446e-11,"numSamples":18},"times":{"cycle":0.3731666666666667,"elapsed":6.866,"period":0.0003823428961748634,"timeStamp":1708944549047}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":976,"cycles":2,"hz":2615.4533273782936,"stats":{"moe":0.000002925901710695216,"rme":0.7652559364819644,"sem":0.0000013866832752110029,"deviation":0.00000588319888355803,"mean":0.0003823428961748634,"variance":3.4612029103498446e-11,"numSamples":18},"times":{"cycle":0.3731666666666667,"elapsed":6.866,"period":0.0003823428961748634,"timeStamp":1708944549047}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":246,"cycles":4,"hz":3184.409440025178,"stats":{"moe":0.000002762821148546372,"rme":0.8797953746532271,"sem":0.0000014096026268093735,"deviation":0.000011623881040966319,"mean":0.0003140299697114619,"variance":1.351146104545362e-10,"numSamples":68},"times":{"cycle":0.07725137254901963,"elapsed":6.012,"period":0.0003140299697114619,"timeStamp":1708944488766}}}},{"name":"optimized.forEach(value => sum += value)","code":"let sum = 0;\noptimized.forEach(value => sum += value);","results":{"aborted":false,"count":4727,"cycles":7,"hz":61123.02268018599,"stats":{"moe":1.1800157294458722e-7,"rme":0.7212612819389627,"sem":6.020488415540165e-8,"deviation":4.89106791025142e-7,"mean":0.000016360447441094337,"variance":2.3922545302691193e-13,"numSamples":66},"times":{"cycle":0.07733583505405293,"elapsed":6.076,"period":0.000016360447441094337,"timeStamp":1708944555918}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":4727,"cycles":7,"hz":61123.02268018599,"stats":{"moe":1.1800157294458722e-7,"rme":0.7212612819389627,"sem":6.020488415540165e-8,"deviation":4.89106791025142e-7,"mean":0.000016360447441094337,"variance":2.3922545302691193e-13,"numSamples":66},"times":{"cycle":0.07733583505405293,"elapsed":6.076,"period":0.000016360447441094337,"timeStamp":1708944555918}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":3660,"cycles":3,"hz":44785.391566265054,"stats":{"moe":2.467246012709839e-7,"rme":1.104965787695163,"sem":1.2587989860764483e-7,"deviation":0.0000010148761879045202,"mean":0.0000223287095418243,"variance":1.029973676775611e-12,"numSamples":65},"times":{"cycle":0.08172307692307694,"elapsed":5.802,"period":0.0000223287095418243,"timeStamp":1708944494783}}}},{"name":"clz.forEach(value => sum += value)","code":"let sum = 0;\nclz.forEach(value => sum += value);","results":{"aborted":false,"count":4734,"cycles":5,"hz":60856.127286376875,"stats":{"moe":1.5705312784826901e-7,"rme":0.955764513905788,"sem":8.012914686136174e-8,"deviation":6.509722664324179e-7,"mean":0.00001643219909959433,"variance":4.2376489166415887e-13,"numSamples":66},"times":{"cycle":0.07779003053747956,"elapsed":6.03,"period":0.00001643219909959433,"timeStamp":1708944561999}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0":{"aborted":false,"count":4734,"cycles":5,"hz":60856.127286376875,"stats":{"moe":1.5705312784826901e-7,"rme":0.955764513905788,"sem":8.012914686136174e-8,"deviation":6.509722664324179e-7,"mean":0.00001643219909959433,"variance":4.2376489166415887e-13,"numSamples":66},"times":{"cycle":0.07779003053747956,"elapsed":6.03,"period":0.00001643219909959433,"timeStamp":1708944561999}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36":{"aborted":false,"count":3853,"cycles":3,"hz":45458.80114176973,"stats":{"moe":2.7748796605120395e-7,"rme":1.261427026795583,"sem":1.4157549288326733e-7,"deviation":0.0000011147665457299501,"mean":0.000021997940440209983,"variance":1.242704451478685e-12,"numSamples":62},"times":{"cycle":0.08475806451612906,"elapsed":5.731,"period":0.000021997940440209983,"timeStamp":1708944500591}}}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment