Skip to content

Instantly share code, notes, and snippets.

@JobLeonard
Last active August 8, 2024 12:11
Show Gist options
  • Save JobLeonard/67df6b8dcbacf4ab764d0b950ca4c293 to your computer and use it in GitHub Desktop.
Save JobLeonard/67df6b8dcbacf4ab764d0b950ca4c293 to your computer and use it in GitHub Desktop.
My Test
{"title":"My Test","initialization":"class ScaledNr {\n\t/** @param {number} v */\n\tconstructor(v) { this.v = v; }\n\t// TS doesn't support valueOf or Symbol.toPrimitive\n\t// https://github.com/microsoft/TypeScript/issues/2361\n\t// https://github.com/microsoft/TypeScript/issues/4538\n\t[Symbol.toPrimitive]() { return this.v; }\n\tvalueOf() { return this.v; }\n}\n\nclass ScaledArr {\n\t/** @param {Array<number>} v */\n\tconstructor(v) { this.v = v; }\n\t// TS doesn't support valueOf or Symbol.toPrimitive\n\t// https://github.com/microsoft/TypeScript/issues/2361\n\t// https://github.com/microsoft/TypeScript/issues/4538\n\t[Symbol.toPrimitive]() { return this.v; }\n\tvalueOf() { return this.v; }\n \n \tmap(cb) { return this.v.map(cb); }\n}\n\nconst {random, round} = Math;\n\nconst xTickVals = [-0],\n xTickValsBoxed = [new ScaledNr(-0)],\n xTickValsBoxedArr = new ScaledArr([-0]);\n\nfor (let i = 0; i < 10000; i++) {\n const v = random();\n xTickValsBoxed[i] = new ScaledNr(v);\n xTickValsBoxedArr.v[i] = xTickVals[i] = v;\n}\n\nconst mainPlotXMargin = 50;\nconst mainPlot = {x: 0, y: 0, w: 1000, h: 10000};\nconst x0 = 10;\n\nconst projectBoxed = (x, i) => {\n if (x instanceof ScaledNr)\n return round(x0 + mainPlotXMargin + x.v * mainPlot.w) + 0.5;\n return round(x0 + (0.5 + i) * mainPlot.w / xTickValsBoxed.length + 0.5);\n};\n\nconst projectUnboxed = (x, i) => round(x0 + (0.5 + i) * mainPlot.w / xTickVals.length + 0.5);\n\nconst projectUnboxedScaled = (x, i) => round(x0 + mainPlotXMargin + x * mainPlot.w) + 0.5;\n\nlet output = [], maybeBoxed = [];\n","setup":"// runs before each test\nfor (let i = 0; i < 10000; i++) {\n const v = random();\n xTickValsBoxed[i] = new ScaledNr(v);\n xTickValsBoxedArr.v[i] = xTickVals[i] = v;\n}\n\nmaybeBoxed = random() < 0.5 ? xTickVals : xTickValsBoxedArr;","tests":[{"name":"test instanceOf for every value","code":"output = xTickValsBoxed.map(projectBoxed);","results":{"aborted":false,"count":976,"cycles":2,"hz":3280.409731113957,"stats":{"moe":0.000004523365939955789,"rme":1.4838493646820403,"sem":0.0000021684400479174446,"deviation":0.000009937040659555358,"mean":0.00030483996877439496,"variance":9.87447770696564e-11,"numSamples":21},"times":{"cycle":0.29752380952380947,"elapsed":6.493,"period":0.00030483996877439496,"timeStamp":1723119100446}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36":{"aborted":false,"count":976,"cycles":2,"hz":3280.409731113957,"stats":{"moe":0.000004523365939955789,"rme":1.4838493646820403,"sem":0.0000021684400479174446,"deviation":0.000009937040659555358,"mean":0.00030483996877439496,"variance":9.87447770696564e-11,"numSamples":21},"times":{"cycle":0.29752380952380947,"elapsed":6.493,"period":0.00030483996877439496,"timeStamp":1723119100446}}}},{"name":"test instanceOf once for the array","code":"if (maybeBoxed instanceof ScaledArr) {\n output = maybeBoxed.map(projectUnboxedScaled);\n} else {\n output = maybeBoxed.map(projectUnboxed);\n}\n","results":{"aborted":false,"count":413,"cycles":4,"hz":4662.946661664854,"stats":{"moe":0.0000063056203764481935,"rme":2.9402771484084984,"sem":0.000003217153253289895,"deviation":0.00002450110922384776,"mean":0.00021445666711593072,"variance":6.003043531989179e-10,"numSamples":58},"times":{"cycle":0.08857060351887938,"elapsed":5.988,"period":0.00021445666711593072,"timeStamp":1723119106945}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36":{"aborted":false,"count":413,"cycles":4,"hz":4662.946661664854,"stats":{"moe":0.0000063056203764481935,"rme":2.9402771484084984,"sem":0.000003217153253289895,"deviation":0.00002450110922384776,"mean":0.00021445666711593072,"variance":6.003043531989179e-10,"numSamples":58},"times":{"cycle":0.08857060351887938,"elapsed":5.988,"period":0.00021445666711593072,"timeStamp":1723119106945}}}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment