Skip to content

Instantly share code, notes, and snippets.

@greggman
Last active November 15, 2022 02:22
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/ddc192654e610293525c847b1a42e39f to your computer and use it in GitHub Desktop.
Save greggman/ddc192654e610293525c847b1a42e39f to your computer and use it in GitHub Desktop.
Access function by string
{"title":"Access function by string","initialization":"\nclass ByClass {\n #count = 0;\n by2(v) {\n this.#count += v * 2;\n }\n by4(v) {\n this.#count += v * 4;\n }\n by8(v) {\n this.#count += v * 8;\n }\n by16(v) {\n this.#count += v * 16;\n }\n add(name, v) {\n this[name](v);\n }\n getResult() {\n return this.#count;\n }\n}\n\nclass ByClassFn {\n count = 0;\n fns = {\n by2(v) { this.count += v * 2 },\n by4(v) { this.count += v * 4 },\n by8(v) { this.count += v * 8 },\n by16(v) { this.count += v * 16 },\n };\n add(name, v) {\n this.fns[name].call(this, v);\n }\n getResult() {\n return this.count;\n }\n}\n\nclass ByClassArrowFn {\n count = 0;\n fns = {\n by2: (v) => { this.count += v * 2 },\n by4: (v) => { this.count += v * 4 },\n by8: (v) => { this.count += v * 8 },\n by16: (v) => { this.count += v * 16 },\n };\n add(name, v) {\n this.fns[name](v);\n }\n getResult() {\n return this.count;\n }\n}\n\nclass ByClassStaticFn {\n count = 0;\n static fns = {\n by2(v) { this.count += v * 2 },\n by4(v) { this.count += v * 4 },\n by8(v) { this.count += v * 8 },\n by16(v) { this.count += v * 16 },\n };\n\n add(name, v) {\n ByClassStaticFn.fns[name].call(this, v);\n }\n getResult() {\n return this.count;\n }\n}\n\nclass ByClassStaticMapFn {\n count = 0;\n static fns = new Map([\n [ 'by2', function(v) { this.count += v * 2 }, ],\n [ 'by4', function(v) { this.count += v * 4 }, ],\n [ 'by8', function(v) { this.count += v * 8 }, ],\n [ 'by16', function(v) { this.count += v * 16 }, ],\n ]);\n\n add(name, v) {\n ByClassStaticMapFn.fns.get(name).call(this, v);\n }\n getResult() {\n return this.count;\n }\n}\n\n\nconst data = [];\nfor (let i = 0; i < 1000; ++i) {\n const v = 2 ** ((i * 13) % 4 + 1);\n const name = `by${v}`;\n data.push([name, i * 27 % 19 + 1]);\n}\n\nfunction test(Ctor) {\n const c = new Ctor();\n\n for (const [name, value] of data) {\n c.add(name, value);\n }\n const actual = c.getResult();\n const expected = 75130;\n if (actual !== expected) {\n throw new Error(`expected: ${expected}, was: ${actual}`);\n }\n}","setup":"// runs before each test","tests":[{"name":"By Class (calling methods by name on class)","code":"test(ByClass);","results":{"aborted":false,"count":4482,"cycles":4,"hz":59073.03220083766,"stats":{"moe":3.551530352729587e-8,"rme":0.20979966688904722,"sem":1.8120052820048912e-8,"deviation":1.5160323873220974e-7,"mean":0.00001692819824450826,"variance":2.2983541994095382e-14,"numSamples":70},"times":{"cycle":0.07587218453188602,"elapsed":6.469,"period":0.00001692819824450826,"timeStamp":1668478896368}},"platforms":{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36":{"aborted":false,"count":5876,"cycles":5,"hz":76642.96091182982,"stats":{"moe":2.1425450381914212e-8,"rme":0.16421099561394004,"sem":1.0931352235670516e-8,"deviation":9.080263133478356e-8,"mean":0.000013047512623506305,"variance":8.245117857320616e-15,"numSamples":69},"times":{"cycle":0.07666718417572305,"elapsed":6.386,"period":0.000013047512623506305,"timeStamp":1668478613201}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:106.0) Gecko/20100101 Firefox/106.0":{"aborted":false,"count":1687,"cycles":3,"hz":22070.585457710236,"stats":{"moe":1.058626440294763e-7,"rme":0.2336450531831715,"sem":5.401155307626342e-8,"deviation":4.486536556565559e-7,"mean":0.00004530917414565709,"variance":2.0129010273399144e-13,"numSamples":69},"times":{"cycle":0.07643657678372351,"elapsed":5.936,"period":0.00004530917414565709,"timeStamp":1668478853680}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15":{"aborted":false,"count":4482,"cycles":4,"hz":59073.03220083766,"stats":{"moe":3.551530352729587e-8,"rme":0.20979966688904722,"sem":1.8120052820048912e-8,"deviation":1.5160323873220974e-7,"mean":0.00001692819824450826,"variance":2.2983541994095382e-14,"numSamples":70},"times":{"cycle":0.07587218453188602,"elapsed":6.469,"period":0.00001692819824450826,"timeStamp":1668478896368}}}},{"name":"By Class Fn (calling named functions on a class property)","code":"test(ByClassFn);","results":{"aborted":false,"count":7504,"cycles":7,"hz":98664.05730007969,"stats":{"moe":1.5834461439995992e-8,"rme":0.1562292210831667,"sem":8.078806857140813e-9,"deviation":6.710760982343205e-8,"mean":0.00001013540317887568,"variance":4.503431296213994e-15,"numSamples":69},"times":{"cycle":0.0760560654542831,"elapsed":6.762,"period":0.00001013540317887568,"timeStamp":1668478902853}},"platforms":{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36":{"aborted":false,"count":6106,"cycles":4,"hz":79780.51715508848,"stats":{"moe":2.492133265902055e-8,"rme":0.1988236807730656,"sem":1.2714965642357423e-8,"deviation":1.0485029273907832e-7,"mean":0.000012534388540701744,"variance":1.099358388747042e-14,"numSamples":68},"times":{"cycle":0.07653497642952485,"elapsed":6.311,"period":0.000012534388540701744,"timeStamp":1668478619598}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:106.0) Gecko/20100101 Firefox/106.0":{"aborted":false,"count":976,"cycles":2,"hz":7817.950118332423,"stats":{"moe":3.325617563700888e-7,"rme":0.2599951222566374,"sem":1.6967436549494326e-7,"deviation":0.0000011254924137767033,"mean":0.00012791076751117735,"variance":1.2667331734689101e-12,"numSamples":44},"times":{"cycle":0.12484090909090909,"elapsed":5.829,"period":0.00012791076751117735,"timeStamp":1668478859623}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15":{"aborted":false,"count":7504,"cycles":7,"hz":98664.05730007969,"stats":{"moe":1.5834461439995992e-8,"rme":0.1562292210831667,"sem":8.078806857140813e-9,"deviation":6.710760982343205e-8,"mean":0.00001013540317887568,"variance":4.503431296213994e-15,"numSamples":69},"times":{"cycle":0.0760560654542831,"elapsed":6.762,"period":0.00001013540317887568,"timeStamp":1668478902853}}}},{"name":"By Class Arrow Fn (calling per instanced closures on a call property)","code":"test(ByClassArrowFn);","results":{"aborted":false,"count":7823,"cycles":5,"hz":102849.77211019894,"stats":{"moe":1.3684470170636625e-8,"rme":0.1407444638498792,"sem":6.981872536039095e-9,"deviation":5.799578901571468e-8,"mean":0.000009722918966981712,"variance":3.3635115435552914e-15,"numSamples":69},"times":{"cycle":0.07606239507869793,"elapsed":6.523,"period":0.000009722918966981712,"timeStamp":1668478909632}},"platforms":{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36":{"aborted":false,"count":6275,"cycles":4,"hz":82830.26505684823,"stats":{"moe":5.489231894325175e-8,"rme":0.4546745327654594,"sem":2.8006285175128444e-8,"deviation":2.343173929774385e-7,"mean":0.00001207288180610889,"variance":5.4904640651743345e-14,"numSamples":70},"times":{"cycle":0.07575733333333329,"elapsed":6.514,"period":0.00001207288180610889,"timeStamp":1668478625916}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:106.0) Gecko/20100101 Firefox/106.0":{"aborted":false,"count":1704,"cycles":4,"hz":21757.820480770737,"stats":{"moe":3.936022199834578e-7,"rme":0.8563926443232907,"sem":2.0081745917523358e-7,"deviation":0.0000016314487506610118,"mean":0.0000459604858346812,"variance":2.661625026033376e-12,"numSamples":66},"times":{"cycle":0.07831666786229677,"elapsed":6.031,"period":0.0000459604858346812,"timeStamp":1668478865459}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15":{"aborted":false,"count":7823,"cycles":5,"hz":102849.77211019894,"stats":{"moe":1.3684470170636625e-8,"rme":0.1407444638498792,"sem":6.981872536039095e-9,"deviation":5.799578901571468e-8,"mean":0.000009722918966981712,"variance":3.3635115435552914e-15,"numSamples":69},"times":{"cycle":0.07606239507869793,"elapsed":6.523,"period":0.000009722918966981712,"timeStamp":1668478909632}}}},{"name":"By Class Static Fn (calling named functions on a class static property)","code":"test(ByClassStaticFn);","results":{"aborted":false,"count":8108,"cycles":7,"hz":107614.71368979906,"stats":{"moe":1.4663239770477672e-8,"rme":0.15779803496648293,"sem":7.481244780855955e-9,"deviation":6.259258456858857e-8,"mean":0.000009292409613080552,"variance":3.917831642975913e-15,"numSamples":70},"times":{"cycle":0.07534285714285711,"elapsed":6.705,"period":0.000009292409613080552,"timeStamp":1668478916169}},"platforms":{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36":{"aborted":false,"count":5686,"cycles":4,"hz":74955.80871031168,"stats":{"moe":1.9006078896941078e-8,"rme":0.14246160141322073,"sem":9.696979029051571e-9,"deviation":8.113074731746662e-8,"mean":0.00001334119419436575,"variance":6.582198160290616e-15,"numSamples":70},"times":{"cycle":0.07585803018916366,"elapsed":6.447,"period":0.00001334119419436575,"timeStamp":1668478632437}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:106.0) Gecko/20100101 Firefox/106.0":{"aborted":false,"count":976,"cycles":2,"hz":7847.953216374267,"stats":{"moe":2.8528175023023104e-7,"rme":0.22388778292922218,"sem":1.4555191338277094e-7,"deviation":9.6548216841792e-7,"mean":0.0001274217585692996,"variance":9.321558175329689e-13,"numSamples":44},"times":{"cycle":0.1243636363636364,"elapsed":5.815,"period":0.0001274217585692996,"timeStamp":1668478871496}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15":{"aborted":false,"count":8108,"cycles":7,"hz":107614.71368979906,"stats":{"moe":1.4663239770477672e-8,"rme":0.15779803496648293,"sem":7.481244780855955e-9,"deviation":6.259258456858857e-8,"mean":0.000009292409613080552,"variance":3.917831642975913e-15,"numSamples":70},"times":{"cycle":0.07534285714285711,"elapsed":6.705,"period":0.000009292409613080552,"timeStamp":1668478916169}}}},{"name":"By Class Static Map Fn (calling functions by name via Map)","code":"test(ByClassStaticMapFn);","results":{"aborted":false,"count":4712,"cycles":5,"hz":60799.73703987895,"stats":{"moe":5.596421469628383e-7,"rme":3.402609537177386,"sem":2.855317076341012e-7,"deviation":0.000002371804496253172,"mean":0.00001644743955626146,"variance":5.625456568446762e-12,"numSamples":69},"times":{"cycle":0.077500335189104,"elapsed":6.761,"period":0.00001644743955626146,"timeStamp":1668478922893}},"platforms":{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36":{"aborted":false,"count":4198,"cycles":5,"hz":55676.24486003592,"stats":{"moe":2.7549536206280878e-8,"rme":0.15338547236013195,"sem":1.405588581953106e-8,"deviation":1.1759997802728793e-7,"mean":0.00001796098142958262,"variance":1.3829754832018603e-14,"numSamples":70},"times":{"cycle":0.07540020004138784,"elapsed":6.441,"period":0.00001796098142958262,"timeStamp":1668478638889}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:106.0) Gecko/20100101 Firefox/106.0":{"aborted":false,"count":523,"cycles":4,"hz":6900.616380620183,"stats":{"moe":3.72165171815198e-7,"rme":0.256816908092428,"sem":1.8988018970163164e-7,"deviation":0.0000015772633148709843,"mean":0.0001449145909354443,"variance":2.487759564437806e-12,"numSamples":69},"times":{"cycle":0.07579033105923737,"elapsed":5.964,"period":0.0001449145909354443,"timeStamp":1668478877318}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15":{"aborted":false,"count":4712,"cycles":5,"hz":60799.73703987895,"stats":{"moe":5.596421469628383e-7,"rme":3.402609537177386,"sem":2.855317076341012e-7,"deviation":0.000002371804496253172,"mean":0.00001644743955626146,"variance":5.625456568446762e-12,"numSamples":69},"times":{"cycle":0.077500335189104,"elapsed":6.761,"period":0.00001644743955626146,"timeStamp":1668478922893}}}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment