Created
August 8, 2024 11:02
-
-
Save JobLeonard/330afad42e457bde73e5c143c71f4dce to your computer and use it in GitHub Desktop.
isTypedArray - instanceOf vs .constructor checks
This file contains 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":"isTypedArray - instanceOf vs .constructor checks","initialization":"// runs once\n\nfunction isTypedArraySwitch(v) {\n\tswitch(v.constructor){\n\t\tcase Uint8ClampedArray:\n\t\tcase Uint8Array:\n\t\tcase Uint16Array:\n\t\tcase Uint32Array:\n\t\tcase Int8Array:\n\t\tcase Int16Array:\n\t\tcase Int32Array:\n\t\tcase Float32Array:\n\t\tcase Float64Array:\n\t\tcase BigInt64Array:\n\t\tcase BigUint64Array:\n\t\treturn true;\n\t\tdefault: return false;\n}\n}\n\nconst isTypedArrayIndexOf = (() => {\n\tconst taConstructors = [Uint8ClampedArray, Uint8Array, Uint16Array, Uint32Array, Int8Array, Int16Array, Int32Array, Float32Array, Float64Array, BigInt64Array, BigUint64Array];\n\treturn function isTypedArrayIndexOf(v = 0) {\n\t\treturn taConstructors.indexOf(v.constructor) !== -1\n\t}\n})();\n\nconst isTypedArrayMap = (() => {\n\tconst taConstructors = new Map();\n\t[Uint8ClampedArray, Uint8Array, Uint16Array, Uint32Array, Int8Array, Int16Array, Int32Array, Float32Array, Float64Array, BigInt64Array, BigUint64Array].map(constr => taConstructors.set(constr, true));\n\t\n\treturn function isTypedArrayMap(v = 0) {\n\t\treturn taConstructors.get(v.constructor) === true;\n\t}\n})();\n\n\n\nconst isTypedArrayWeakMap = (() => {\n\tconst taConstructors = new WeakMap();\n\t[Uint8ClampedArray, Uint8Array, Uint16Array, Uint32Array, Int8Array, Int16Array, Int32Array, Float32Array, Float64Array, BigInt64Array, BigUint64Array].map(constr => taConstructors.set(constr, true));\n\t\n\treturn function isTypedArrayMap(v = 0) {\n\t\treturn taConstructors.get(v.constructor) === true;\n\t}\n})();\n\nfunction isTypedArrayInstanceOf(v) {\n\treturn (v instanceof Uint8ClampedArray) ||\n (v instanceof Uint8Array) ||\n (v instanceof Uint16Array) ||\n (v instanceof Uint32Array) ||\n (v instanceof Int8Array) ||\n (v instanceof Int16Array) ||\n (v instanceof Int32Array) ||\n (v instanceof Float32Array) ||\n (v instanceof Float64Array) ||\n (v instanceof BigInt64Array) ||\n (v instanceof BigUint64Array);\n}\n\nfunction isTypedArrayHasArrayBuffer(v) {\n return v.buffer ? (v.buffer instanceof ArrayBuffer) : false;\n}\n\nfunction isTypedArrayHasArrayBuffer2(v) {\n return v.buffer ? (v.buffer.constructor === ArrayBuffer) : false;\n}\n\nfunction isTypedArrayHasArrayBuffer3(v) {\n return (v.buffer ?? \"\").constructor === ArrayBuffer;\n}\n\nfunction isTypedArrayHasArrayBuffer4(v) {\n return v?.buffer?.constructor === ArrayBuffer;\n}\n\nfunction generateRandomTypedArrays(){\n\tconst taConstructors = [Uint8ClampedArray, Uint8Array, Uint16Array, Uint32Array, Int8Array, Int16Array, Int32Array, Float32Array, Float64Array, BigInt64Array, BigUint64Array];\n\n\tconst arrays = [];\n\tfor (let i = 0; i < 1000; i++) {\n\t\tarrays[i] = Math.random() < 0.5 ? new (taConstructors[taConstructors.length * Math.random() | 0])() : 0;\n\t}\n\treturn arrays;\n}","setup":"// runs before each test\n\nconst arrays = generateRandomTypedArrays();\nconst isTA = [false];\nfor (let i = 0; i < arrays.length; i++) isTA[i] = false;","tests":[{"name":"Constructor equality + switch","code":"// Constructor equality + switch\nfor (let i = 0; i < arrays.length; i++) isTA[i] = isTypedArraySwitch(arrays[i]);","results":{"aborted":false,"count":5001,"cycles":4,"hz":61465.95916017787,"stats":{"moe":2.532274102034593e-7,"rme":1.5564865653803437,"sem":1.2919765826707107e-7,"deviation":0.0000010335812661365686,"mean":0.000016269167742002356,"variance":1.0682902337084723e-12,"numSamples":64},"times":{"cycle":0.08136210787775378,"elapsed":6.056,"period":0.000016269167742002356,"timeStamp":1723114791460}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0":{"aborted":false,"count":3451,"cycles":3,"hz":15771.343182393464,"stats":{"moe":0.00004713800421413532,"rme":74.34296413942376,"sem":0.000022882526317541418,"deviation":0.00011667844821344413,"mean":0.00006340614039242786,"variance":1.3613860277497364e-8,"numSamples":26},"times":{"cycle":0.21881459049426855,"elapsed":6.264,"period":0.00006340614039242786,"timeStamp":1723114709028}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36":{"aborted":false,"count":5001,"cycles":4,"hz":61465.95916017787,"stats":{"moe":2.532274102034593e-7,"rme":1.5564865653803437,"sem":1.2919765826707107e-7,"deviation":0.0000010335812661365686,"mean":0.000016269167742002356,"variance":1.0682902337084723e-12,"numSamples":64},"times":{"cycle":0.08136210787775378,"elapsed":6.056,"period":0.000016269167742002356,"timeStamp":1723114791460}}}},{"name":"Constructor equality + Array.indexOf","code":"// Constructor equality + Array.indexOf\nfor (let i = 0; i < arrays.length; i++) isTA[i] = isTypedArrayIndexOf(arrays[i]);","results":{"aborted":false,"count":2977,"cycles":4,"hz":36669.97628192348,"stats":{"moe":4.628099832327097e-7,"rme":1.697123110818087,"sem":2.361275424656682e-7,"deviation":0.0000018890203397253457,"mean":0.000027270265797607057,"variance":3.5683978438960606e-12,"numSamples":64},"times":{"cycle":0.08118358127947621,"elapsed":6.004,"period":0.000027270265797607057,"timeStamp":1723114797521}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0":{"aborted":false,"count":2213,"cycles":3,"hz":27534.80003567169,"stats":{"moe":3.813814804770639e-7,"rme":1.0501262802244382,"sem":1.94582387998502e-7,"deviation":0.000001556659103988016,"mean":0.00003631767794588982,"variance":2.4231875660287728e-12,"numSamples":64},"times":{"cycle":0.08037102129425418,"elapsed":5.871,"period":0.00003631767794588982,"timeStamp":1723114715298}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36":{"aborted":false,"count":2977,"cycles":4,"hz":36669.97628192348,"stats":{"moe":4.628099832327097e-7,"rme":1.697123110818087,"sem":2.361275424656682e-7,"deviation":0.0000018890203397253457,"mean":0.000027270265797607057,"variance":3.5683978438960606e-12,"numSamples":64},"times":{"cycle":0.08118358127947621,"elapsed":6.004,"period":0.000027270265797607057,"timeStamp":1723114797521}}}},{"name":"Constructor equality + Map","code":"// Constructor equality + Map\nfor (let i = 0; i < arrays.length; i++) isTA[i] = isTypedArrayMap(arrays[i]);","results":{"aborted":false,"count":3198,"cycles":4,"hz":39943.80616945425,"stats":{"moe":3.34627258168187e-7,"rme":1.3366286339285989,"sem":1.7072819294295255e-7,"deviation":0.0000013764546964073289,"mean":0.000025035170553293893,"variance":1.8946275312617917e-12,"numSamples":65},"times":{"cycle":0.08006247542943387,"elapsed":6.018,"period":0.000025035170553293893,"timeStamp":1723114803530}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0":{"aborted":false,"count":2443,"cycles":4,"hz":31453.494991597083,"stats":{"moe":2.786421192088895e-7,"rme":0.8764268500984804,"sem":1.4216434653514773e-7,"deviation":0.000001163665327972907,"mean":0.000031792969279476055,"variance":1.3541169955262934e-12,"numSamples":67},"times":{"cycle":0.07767022394976,"elapsed":6.097,"period":0.000031792969279476055,"timeStamp":1723114721174}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36":{"aborted":false,"count":3198,"cycles":4,"hz":39943.80616945425,"stats":{"moe":3.34627258168187e-7,"rme":1.3366286339285989,"sem":1.7072819294295255e-7,"deviation":0.0000013764546964073289,"mean":0.000025035170553293893,"variance":1.8946275312617917e-12,"numSamples":65},"times":{"cycle":0.08006247542943387,"elapsed":6.018,"period":0.000025035170553293893,"timeStamp":1723114803530}}}},{"name":"instanceof","code":"// instanceof\nfor (let i = 0; i < arrays.length; i++) isTA[i] = isTypedArrayInstanceOf(arrays[i]);","results":{"aborted":false,"count":4222,"cycles":8,"hz":52578.08539560226,"stats":{"moe":1.5686709634463457e-7,"rme":0.8247771587368363,"sem":8.003423282889519e-8,"deviation":6.452566137538903e-7,"mean":0.000019019330819597362,"variance":4.163560975931371e-13,"numSamples":65},"times":{"cycle":0.08029961472034006,"elapsed":6.127,"period":0.000019019330819597362,"timeStamp":1723114809553}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0":{"aborted":false,"count":1734,"cycles":7,"hz":12528.470669189746,"stats":{"moe":0.00003295505920109958,"rme":41.28764926023877,"sem":0.000016813805714846726,"deviation":0.00010896591499132985,"mean":0.00007981820179052014,"variance":1.1873570629897723e-8,"numSamples":42},"times":{"cycle":0.13840476190476192,"elapsed":6.568,"period":0.00007981820179052014,"timeStamp":1723114727276}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36":{"aborted":false,"count":4222,"cycles":8,"hz":52578.08539560226,"stats":{"moe":1.5686709634463457e-7,"rme":0.8247771587368363,"sem":8.003423282889519e-8,"deviation":6.452566137538903e-7,"mean":0.000019019330819597362,"variance":4.163560975931371e-13,"numSamples":65},"times":{"cycle":0.08029961472034006,"elapsed":6.127,"period":0.000019019330819597362,"timeStamp":1723114809553}}}},{"name":"ArrayBuffer instanceof","code":"// ArrayBuffer instanceof\nfor (let i = 0; i < arrays.length; i++) isTA[i] = isTypedArrayHasArrayBuffer(arrays[i]);","results":{"aborted":false,"count":2257,"cycles":5,"hz":27178.40264575983,"stats":{"moe":4.990608770044473e-7,"rme":1.3563677459972894,"sem":2.5462289643084046e-7,"deviation":0.000002021006586176879,"mean":0.000036793921005361676,"variance":4.084467621370322e-12,"numSamples":63},"times":{"cycle":0.0830438797091013,"elapsed":5.963,"period":0.000036793921005361676,"timeStamp":1723114815685}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0":{"aborted":false,"count":1983,"cycles":4,"hz":19679.926298625887,"stats":{"moe":0.0000034925323739063333,"rme":6.873277971404154,"sem":0.0000017819042724011905,"deviation":0.000012345394936091657,"mean":0.00005081319842492617,"variance":1.5240877612807756e-10,"numSamples":48},"times":{"cycle":0.10076257247662859,"elapsed":5.938,"period":0.00005081319842492617,"timeStamp":1723114733850}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36":{"aborted":false,"count":2257,"cycles":5,"hz":27178.40264575983,"stats":{"moe":4.990608770044473e-7,"rme":1.3563677459972894,"sem":2.5462289643084046e-7,"deviation":0.000002021006586176879,"mean":0.000036793921005361676,"variance":4.084467621370322e-12,"numSamples":63},"times":{"cycle":0.0830438797091013,"elapsed":5.963,"period":0.000036793921005361676,"timeStamp":1723114815685}}}},{"name":"ArrayBuffer constructor + ternary","code":"// ArrayBuffer constructor + ternary\nfor (let i = 0; i < arrays.length; i++) isTA[i] = isTypedArrayHasArrayBuffer2(arrays[i]);","results":{"aborted":false,"count":2215,"cycles":4,"hz":27761.60780758342,"stats":{"moe":4.2265369859398414e-7,"rme":1.1733546218790758,"sem":2.1563964213978782e-7,"deviation":0.0000017251171371183026,"mean":0.00003602096848752535,"variance":2.9760291367792483e-12,"numSamples":64},"times":{"cycle":0.07978644519986865,"elapsed":5.82,"period":0.00003602096848752535,"timeStamp":1723114821654}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0":{"aborted":false,"count":1997,"cycles":3,"hz":18375.762102215336,"stats":{"moe":0.000003758066145507095,"rme":6.905732945422774,"sem":0.0000019173806864832116,"deviation":0.00001328400306576086,"mean":0.000054419511660930925,"variance":1.764647374511439e-10,"numSamples":48},"times":{"cycle":0.10867576478687906,"elapsed":5.86,"period":0.000054419511660930925,"timeStamp":1723114739793}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36":{"aborted":false,"count":2215,"cycles":4,"hz":27761.60780758342,"stats":{"moe":4.2265369859398414e-7,"rme":1.1733546218790758,"sem":2.1563964213978782e-7,"deviation":0.0000017251171371183026,"mean":0.00003602096848752535,"variance":2.9760291367792483e-12,"numSamples":64},"times":{"cycle":0.07978644519986865,"elapsed":5.82,"period":0.00003602096848752535,"timeStamp":1723114821654}}}},{"name":"ArrayBuffer constructor + nullish coalescing operator","code":"// ArrayBuffer constructor + nullish coalescing operator\nfor (let i = 0; i < arrays.length; i++) isTA[i] = isTypedArrayHasArrayBuffer3(arrays[i]);","results":{"aborted":false,"count":3252,"cycles":4,"hz":40196.22006513844,"stats":{"moe":2.4942311596425314e-7,"rme":1.0025866458631663,"sem":1.272566918184965e-7,"deviation":0.000001010068677661589,"mean":0.00002487796112120713,"variance":1.0202387335930307e-12,"numSamples":63},"times":{"cycle":0.08090312956616559,"elapsed":5.881,"period":0.00002487796112120713,"timeStamp":1723114827479}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0":{"aborted":false,"count":2195,"cycles":4,"hz":17730.413285708622,"stats":{"moe":0.000006739728973412412,"rme":11.949818013226675,"sem":0.0000034386372313328632,"deviation":0.000022284916252159412,"mean":0.000056400264555933254,"variance":4.966174923657587e-10,"numSamples":42},"times":{"cycle":0.1237985807002735,"elapsed":5.889,"period":0.000056400264555933254,"timeStamp":1723114745658}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36":{"aborted":false,"count":3252,"cycles":4,"hz":40196.22006513844,"stats":{"moe":2.4942311596425314e-7,"rme":1.0025866458631663,"sem":1.272566918184965e-7,"deviation":0.000001010068677661589,"mean":0.00002487796112120713,"variance":1.0202387335930307e-12,"numSamples":63},"times":{"cycle":0.08090312956616559,"elapsed":5.881,"period":0.00002487796112120713,"timeStamp":1723114827479}}}},{"name":"ArrayBuffer constructor + optional chaining","code":"// ArrayBuffer constructor + optional chaining\nfor (let i = 0; i < arrays.length; i++) isTA[i] = isTypedArrayHasArrayBuffer4(arrays[i]);","results":{"aborted":false,"count":3277,"cycles":4,"hz":41288.80218771943,"stats":{"moe":1.9500627954504013e-7,"rme":0.8051575701498279,"sem":9.949299976787762e-8,"deviation":8.082849511066746e-7,"mean":0.000024219641816042583,"variance":6.533245621855194e-13,"numSamples":66},"times":{"cycle":0.07936776623117155,"elapsed":6.032,"period":0.000024219641816042583,"timeStamp":1723114833366}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0":{"aborted":false,"count":2224,"cycles":4,"hz":18171.98587271593,"stats":{"moe":0.000006537228148947648,"rme":11.879441756939755,"sem":0.000003335320484156963,"deviation":0.00002161534720390944,"mean":0.000055029758827924024,"variance":4.672232347455556e-10,"numSamples":42},"times":{"cycle":0.12238618363330303,"elapsed":5.901,"period":0.000055029758827924024,"timeStamp":1723114751552}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36":{"aborted":false,"count":3277,"cycles":4,"hz":41288.80218771943,"stats":{"moe":1.9500627954504013e-7,"rme":0.8051575701498279,"sem":9.949299976787762e-8,"deviation":8.082849511066746e-7,"mean":0.000024219641816042583,"variance":6.533245621855194e-13,"numSamples":66},"times":{"cycle":0.07936776623117155,"elapsed":6.032,"period":0.000024219641816042583,"timeStamp":1723114833366}}}},{"name":"Constructor equality + WeakMap","code":"// Constructor equality + WeakMap\nfor (let i = 0; i < arrays.length; i++) isTA[i] = isTypedArrayMap(arrays[i]);","results":{"aborted":false,"count":3197,"cycles":4,"hz":40126.18038546936,"stats":{"moe":3.238490724769511e-7,"rme":1.299482629987708,"sem":1.6522911861068935e-7,"deviation":0.0000013321197417635702,"mean":0.0000249213852500679,"variance":1.7745430063962407e-12,"numSamples":65},"times":{"cycle":0.07967366864446708,"elapsed":6.079,"period":0.0000249213852500679,"timeStamp":1723114839404}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0":{"aborted":false,"count":2442,"cycles":4,"hz":30345.88205817641,"stats":{"moe":5.123036936179459e-7,"rme":1.554630746449833,"sem":2.6137943551936015e-7,"deviation":0.000002091035484154881,"mean":0.00003295340033560038,"variance":4.372429395994838e-12,"numSamples":64},"times":{"cycle":0.08047220361953612,"elapsed":5.999,"period":0.00003295340033560038,"timeStamp":1723114757458}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36":{"aborted":false,"count":3197,"cycles":4,"hz":40126.18038546936,"stats":{"moe":3.238490724769511e-7,"rme":1.299482629987708,"sem":1.6522911861068935e-7,"deviation":0.0000013321197417635702,"mean":0.0000249213852500679,"variance":1.7745430063962407e-12,"numSamples":65},"times":{"cycle":0.07967366864446708,"elapsed":6.079,"period":0.0000249213852500679,"timeStamp":1723114839404}}}}]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment