Skip to content

Instantly share code, notes, and snippets.

@greggman
Last active September 27, 2022 23:55
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 greggman/0e299fd0022fef1c2821aaeb9e0aaf86 to your computer and use it in GitHub Desktop.
Save greggman/0e299fd0022fef1c2821aaeb9e0aaf86 to your computer and use it in GitHub Desktop.
instanceOf vs prototype.toString
{"title":"instanceOf vs prototype.toString","initialization":"function isType(object, type) {\n const s = Object.prototype.toString.call(object);\n return s.substring(8, s.length - 1) === type;\n}\n\nfunction isTextureNew(gl, t) {\n return typeof WebGLTexture !== 'undefined' && isType(t, 'WebGLTexture');\n}\n\nfunction isTextureOld(gl, t) {\n return typeof WebGLTexture !== 'undefined' && t instanceof WebGLTexture;\n}\n\nconst gl = document.createElement('canvas').getContext('webgl2');\nconst tex = gl.createTexture();\nconst smp = gl.createSampler();\n\nconst resources = [];\nfor (let i = 0; i < 1000; ++i) {\n resources.push(i % 3 ? tex : smp);\n}\n\nfunction test(fn) {\n let sum = 0;\n for (let i = 0; i < resources.length; ++i) {\n sum += fn(gl, resources[i]) ? 1 : 0;\n }\n if (sum !== 666) throw Error(`sum = ${sum}`);\n}","setup":"// runs before each test","tests":[{"name":"isTextureOld (instanceOf)","code":"test(isTextureOld)","results":{"aborted":false,"count":976,"cycles":2,"hz":11018.752391886717,"stats":{"moe":8.126769488645376e-7,"rme":0.8954686074132323,"sem":4.1463109635945797e-7,"deviation":0.0000031848418827355495,"mean":0.00009075437621561547,"variance":1.0143217818026519e-11,"numSamples":59},"times":{"cycle":0.0885762711864407,"elapsed":6.88,"period":0.00009075437621561547,"timeStamp":1664322893605}},"platforms":{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36":{"aborted":false,"count":16713,"cycles":4,"hz":216704.48486885105,"stats":{"moe":1.980249041052149e-8,"rme":0.4291288483532422,"sem":1.0103311433939536e-8,"deviation":8.145550089175235e-8,"mean":0.000004614579161133639,"variance":6.634998625526269e-15,"numSamples":65},"times":{"cycle":0.07712346152002651,"elapsed":5.918,"period":0.000004614579161133639,"timeStamp":1664322814133}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15":{"aborted":false,"count":976,"cycles":2,"hz":11018.752391886717,"stats":{"moe":8.126769488645376e-7,"rme":0.8954686074132323,"sem":4.1463109635945797e-7,"deviation":0.0000031848418827355495,"mean":0.00009075437621561547,"variance":1.0143217818026519e-11,"numSamples":59},"times":{"cycle":0.0885762711864407,"elapsed":6.88,"period":0.00009075437621561547,"timeStamp":1664322893605}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:105.0) Gecko/20100101 Firefox/105.0":{"aborted":false,"count":976,"cycles":2,"hz":7818.181818181818,"stats":{"moe":0.000014760338599724367,"rme":11.539901087057231,"sem":0.000007530784999859371,"deviation":0.000049382659676313476,"mean":0.00012790697674418605,"variance":2.4386470767065972e-9,"numSamples":43},"times":{"cycle":0.12483720930232559,"elapsed":5.784,"period":0.00012790697674418605,"timeStamp":1664322858634}}}},{"name":"isTextureNew (prototype.toString)","code":"test(isTextureNew)","results":{"aborted":false,"count":636,"cycles":6,"hz":7670.529944594031,"stats":{"moe":0.0000010891176683649429,"rme":0.8354109688379727,"sem":5.556722797780321e-7,"deviation":0.000004375367906342319,"mean":0.0001303690888665093,"variance":1.914384431585037e-11,"numSamples":62},"times":{"cycle":0.08291474051909992,"elapsed":7.223,"period":0.0001303690888665093,"timeStamp":1664322900514}},"platforms":{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36":{"aborted":false,"count":720,"cycles":8,"hz":9305.223940578802,"stats":{"moe":6.001032385507895e-7,"rme":0.5584095022181679,"sem":3.061751217095865e-7,"deviation":0.0000024873784473127703,"mean":0.00010746651626933313,"variance":6.187051540156088e-12,"numSamples":66},"times":{"cycle":0.07737589171391986,"elapsed":6.261,"period":0.00010746651626933313,"timeStamp":1664322820057}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15":{"aborted":false,"count":636,"cycles":6,"hz":7670.529944594031,"stats":{"moe":0.0000010891176683649429,"rme":0.8354109688379727,"sem":5.556722797780321e-7,"deviation":0.000004375367906342319,"mean":0.0001303690888665093,"variance":1.914384431585037e-11,"numSamples":62},"times":{"cycle":0.08291474051909992,"elapsed":7.223,"period":0.0001303690888665093,"timeStamp":1664322900514}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:105.0) Gecko/20100101 Firefox/105.0":{"aborted":false,"count":976,"cycles":2,"hz":6449.723164850867,"stats":{"moe":0.00002087792293013926,"rme":13.465682315649028,"sem":0.00001065200149496901,"deviation":0.00006479359556627811,"mean":0.00015504541426672572,"variance":4.198210026406415e-9,"numSamples":37},"times":{"cycle":0.1513243243243243,"elapsed":5.949,"period":0.00015504541426672572,"timeStamp":1664322864424}}}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment