Skip to content

Instantly share code, notes, and snippets.

@JobLeonard
Last active September 6, 2022 11:45
Show Gist options
  • Select an option

  • Save JobLeonard/6da24ef651e83e20203ec2d99683a564 to your computer and use it in GitHub Desktop.

Select an option

Save JobLeonard/6da24ef651e83e20203ec2d99683a564 to your computer and use it in GitHub Desktop.
Adding a value to an array if it's not in the array yet
{"title":"Adding a value to an array if it's not in the array yet","initialization":"/*\nInspired by example 1a and example 2 in\nhttps://pic.plover.com/knuth-GOTO.pdf\n*/\n\nconst tarr = new Uint16Array(2000);","setup":"// runs before each test\n\nconst arr = [];\nconst count = []\ntarr.fill(0);","tests":[{"name":"test 1","code":"for (let i = 0; i < 1000; i++) {\n\tconst x = Math.random() * 1000 | 0;\n \tlet j = 0;\n \twhile (j < arr.length && arr[j] !== x) j++;\n \tif (j < arr.length) {\n count[j]++;\n } else {\n arr[j] = x;\n count[j] = 1;\n }\n}","results":{"aborted":false,"count":106,"cycles":3,"hz":1034.9644793067232,"stats":{"moe":0.000027355793219103704,"rme":2.831227428503205,"sem":0.000013957037356685564,"deviation":0.00010350815932898913,"mean":0.0009662167349645234,"variance":1.07139390476754e-8,"numSamples":55},"times":{"cycle":0.10241897390623948,"elapsed":5.879,"period":0.0009662167349645234,"timeStamp":1662464684582}},"platforms":{"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0":{"aborted":false,"count":106,"cycles":3,"hz":1034.9644793067232,"stats":{"moe":0.000027355793219103704,"rme":2.831227428503205,"sem":0.000013957037356685564,"deviation":0.00010350815932898913,"mean":0.0009662167349645234,"variance":1.07139390476754e-8,"numSamples":55},"times":{"cycle":0.10241897390623948,"elapsed":5.879,"period":0.0009662167349645234,"timeStamp":1662464684582}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36":{"aborted":false,"count":172,"cycles":7,"hz":2241.0119936876317,"stats":{"moe":0.000001952707256131113,"rme":0.4376040381150691,"sem":9.962792123117925e-7,"deviation":0.000008032259798929525,"mean":0.0004462269737139957,"variance":6.451719747749938e-11,"numSamples":65},"times":{"cycle":0.07675103947880726,"elapsed":6.145,"period":0.0004462269737139957,"timeStamp":1662464477732}}}},{"name":"test 2","code":"// Sentinel-based\nfor (let i = 0; i < 1000; i++) {\n\tconst x = Math.random() * 1000 | 0, end = arr.length;\n \tarr[end] = x;\n \tlet j = 0;\n \twhile (arr[j] !== x) j++;\n \tif (j === end) {\n count[j] = 1;\n } else {\n count[j]++;\n arr.pop();\n }\n}","results":{"aborted":false,"count":129,"cycles":3,"hz":1373.1697750357241,"stats":{"moe":0.00003440480171726891,"rme":4.724363383425084,"sem":0.00001755347026391271,"deviation":0.00012899113608511113,"mean":0.0007282420704125855,"variance":1.663871318852766e-8,"numSamples":54},"times":{"cycle":0.09394322708322353,"elapsed":5.931,"period":0.0007282420704125855,"timeStamp":1662464690466}},"platforms":{"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0":{"aborted":false,"count":129,"cycles":3,"hz":1373.1697750357241,"stats":{"moe":0.00003440480171726891,"rme":4.724363383425084,"sem":0.00001755347026391271,"deviation":0.00012899113608511113,"mean":0.0007282420704125855,"variance":1.663871318852766e-8,"numSamples":54},"times":{"cycle":0.09394322708322353,"elapsed":5.931,"period":0.0007282420704125855,"timeStamp":1662464690466}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36":{"aborted":false,"count":172,"cycles":7,"hz":2237.859144520066,"stats":{"moe":0.0000011250908939904455,"rme":0.2517794945532774,"sem":5.740259663216559e-7,"deviation":0.000004627945294701332,"mean":0.0004468556488234479,"variance":2.1417877650748194e-11,"numSamples":65},"times":{"cycle":0.07685917159763304,"elapsed":6.071,"period":0.0004468556488234479,"timeStamp":1662464483883}}}},{"name":"test 3","code":"// Typed Array version\nlet length = 0;\nfor (let i = 0; i < 1000; i++) {\n\tconst x = Math.random() * 1000 | 0;\n \tlet j = 0;\n \twhile (j < length && tarr[j] !== x) j += 2;\n \tif (j < length) {\n tarr[j+1]++;\n } else {\n tarr[j] = x;\n tarr[j+1] = 1;\n length += 2;\n }\n}","results":{"aborted":false,"count":976,"cycles":2,"hz":1831.8809491888992,"stats":{"moe":0.00001167198255815957,"rme":2.1381682487557634,"sem":0.000005403695628777579,"deviation":0.000020218777665293688,"mean":0.0005458870023419204,"variance":4.087989702785788e-10,"numSamples":14},"times":{"cycle":0.5327857142857143,"elapsed":7.634,"period":0.0005458870023419204,"timeStamp":1662464696403}},"platforms":{"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0":{"aborted":false,"count":976,"cycles":2,"hz":1831.8809491888992,"stats":{"moe":0.00001167198255815957,"rme":2.1381682487557634,"sem":0.000005403695628777579,"deviation":0.000020218777665293688,"mean":0.0005458870023419204,"variance":4.087989702785788e-10,"numSamples":14},"times":{"cycle":0.5327857142857143,"elapsed":7.634,"period":0.0005458870023419204,"timeStamp":1662464696403}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36":{"aborted":false,"count":295,"cycles":4,"hz":3849.0257310080547,"stats":{"moe":0.0000015741779202903807,"rme":0.6059051320382421,"sem":8.031520001481534e-7,"deviation":0.0000065248376939637854,"mean":0.0002598060054376673,"variance":4.257350693257065e-11,"numSamples":66},"times":{"cycle":0.07664277160411186,"elapsed":6.074,"period":0.0002598060054376673,"timeStamp":1662464489960}}}},{"name":"test 4","code":"// Sentinel-based, typed array\nlet length = 0;\nfor (let i = 0; i < 1000; i++) {\n\tconst x = Math.random() * 1000 | 0;\n \ttarr[length] = x;\n \tlet j = 0;\n \twhile (tarr[j] !== x) j += 2;\n \tif (j === length) {\n tarr[j+1] = 1;\n length += 2;\n } else {\n tarr[j+1]++;\n }\n}","results":{"aborted":false,"count":193,"cycles":4,"hz":2032.179896746266,"stats":{"moe":0.00002497909406280036,"rme":5.07620127933569,"sem":0.000012744435746326715,"deviation":0.00009278089271013326,"mean":0.0004920824192784828,"variance":8.60829405208926e-9,"numSamples":53},"times":{"cycle":0.09497190692074718,"elapsed":5.969,"period":0.0004920824192784828,"timeStamp":1662464704043}},"platforms":{"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0":{"aborted":false,"count":193,"cycles":4,"hz":2032.179896746266,"stats":{"moe":0.00002497909406280036,"rme":5.07620127933569,"sem":0.000012744435746326715,"deviation":0.00009278089271013326,"mean":0.0004920824192784828,"variance":8.60829405208926e-9,"numSamples":53},"times":{"cycle":0.09497190692074718,"elapsed":5.969,"period":0.0004920824192784828,"timeStamp":1662464704043}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36":{"aborted":false,"count":295,"cycles":4,"hz":3856.472027749108,"stats":{"moe":0.0000017253654981015046,"rme":0.6653823781071859,"sem":8.802885194395432e-7,"deviation":0.0000072054720726419606,"mean":0.0002593043571441814,"variance":5.1918827789623234e-11,"numSamples":67},"times":{"cycle":0.07649478535753351,"elapsed":6.118,"period":0.0002593043571441814,"timeStamp":1662464496039}}}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment