Skip to content

Instantly share code, notes, and snippets.

@greggman
Last active Sep 21, 2022
Embed
What would you like to do?
try/catch
{"title":"try/catch","initialization":"function add(a, b) {\n return a + b;\n}\n\nfunction addWithTryCatch(a, b) {\n try {\n return a + b;\n } catch (e) {\n }\n}\n\nfunction addViaFn(a, b) {\n return add(a, b);\n}\n\nfunction addViaFnWithTryCatch(a, b) {\n try {\n return add(a, b);\n } catch (e) {\n }\n}\n\nfunction test(fn) {\n let sum = 0;\n for (let i = 0; i < 1000; ++i) {\n sum += fn(i, i + 1);\n }\n const expected = 1000000;\n if (sum !== expected) {\n throw new Error(`expected: ${expected}, actual: ${sum}`);\n }\n}","setup":"// runs before each test","tests":[{"name":"add","code":"test(add);","results":{"aborted":false,"count":86463,"cycles":4,"hz":1078781.9755107136,"stats":{"moe":6.180276928301153e-9,"rme":0.6667171353916002,"sem":3.153202514439364e-9,"deviation":2.4828341427049394e-8,"mean":9.269713646509372e-7,"variance":6.164465380181371e-16,"numSamples":62},"times":{"cycle":0.08014872510181398,"elapsed":5.891,"period":9.269713646509372e-7,"timeStamp":1663795325040}},"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":56159,"cycles":6,"hz":706033.3051630899,"stats":{"moe":8.081149149162298e-9,"rme":0.5705560443298949,"sem":4.123035280184846e-9,"deviation":3.298428224147877e-8,"mean":0.0000014163637787158007,"variance":1.0879628749855316e-15,"numSamples":64},"times":{"cycle":0.07954157344890064,"elapsed":6.116,"period":0.0000014163637787158007,"timeStamp":1663795130631}},"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":75194,"cycles":5,"hz":930192.3978290466,"stats":{"moe":1.2725621051861256e-8,"rme":1.1837275960094615,"sem":6.492663801970028e-9,"deviation":5.194131041576023e-8,"mean":0.0000010750464122625337,"variance":2.697899727706362e-15,"numSamples":64},"times":{"cycle":0.08083703992366896,"elapsed":6.811,"period":0.0000010750464122625337,"timeStamp":1663795191346}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:104.0) Gecko/20100101 Firefox/104.0":{"aborted":false,"count":86463,"cycles":4,"hz":1078781.9755107136,"stats":{"moe":6.180276928301153e-9,"rme":0.6667171353916002,"sem":3.153202514439364e-9,"deviation":2.4828341427049394e-8,"mean":9.269713646509372e-7,"variance":6.164465380181371e-16,"numSamples":62},"times":{"cycle":0.08014872510181398,"elapsed":5.891,"period":9.269713646509372e-7,"timeStamp":1663795325040}}}},{"name":"addWithTryCatch","code":"test(addWithTryCatch);\n","results":{"aborted":false,"count":86557,"cycles":4,"hz":829123.86700579,"stats":{"moe":3.163725309119673e-7,"rme":26.23120162441392,"sem":1.6141455658773842e-7,"deviation":0.0000012913164527019074,"mean":0.0000012060924064474153,"variance":1.6674981810186375e-12,"numSamples":64},"times":{"cycle":0.10439574042486892,"elapsed":6.127,"period":0.0000012060924064474153,"timeStamp":1663795330937}},"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":12002,"cycles":5,"hz":154221.99403348105,"stats":{"moe":2.900914884073741e-8,"rme":0.4473848779432569,"sem":1.4800586143233372e-8,"deviation":1.2024053023875076e-7,"mean":0.0000064841594499349,"variance":1.4457785112095936e-14,"numSamples":66},"times":{"cycle":0.07782288171811867,"elapsed":6.028,"period":0.0000064841594499349,"timeStamp":1663795136752}},"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":18650,"cycles":5,"hz":237796.611772472,"stats":{"moe":4.330047224395816e-8,"rme":1.0296705587761217,"sem":2.2092077675488855e-8,"deviation":1.7811202441522342e-7,"mean":0.0000042052743836267,"variance":3.1723893241289146e-14,"numSamples":65},"times":{"cycle":0.07842836725463796,"elapsed":6.404,"period":0.0000042052743836267,"timeStamp":1663795198168}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:104.0) Gecko/20100101 Firefox/104.0":{"aborted":false,"count":86557,"cycles":4,"hz":829123.86700579,"stats":{"moe":3.163725309119673e-7,"rme":26.23120162441392,"sem":1.6141455658773842e-7,"deviation":0.0000012913164527019074,"mean":0.0000012060924064474153,"variance":1.6674981810186375e-12,"numSamples":64},"times":{"cycle":0.10439574042486892,"elapsed":6.127,"period":0.0000012060924064474153,"timeStamp":1663795330937}}}},{"name":"addViaFn","code":"test(addViaFn);","results":{"aborted":false,"count":10891,"cycles":3,"hz":62817.67529471275,"stats":{"moe":0.0000065432085986161076,"rme":41.102915313343914,"sem":0.0000031996130066582435,"deviation":0.000017525002190336473,"mean":0.000015919086392618673,"variance":3.071257017712982e-10,"numSamples":30},"times":{"cycle":0.17337476990200998,"elapsed":5.632,"period":0.000015919086392618673,"timeStamp":1663795337070}},"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":12356,"cycles":5,"hz":154454.49357758948,"stats":{"moe":4.0578756869456554e-8,"rme":0.6267571342280043,"sem":2.0703447382375793e-8,"deviation":1.6691652907505056e-7,"mean":0.0000064743988785127494,"variance":2.78611276784622e-14,"numSamples":65},"times":{"cycle":0.07999767254290353,"elapsed":6.368,"period":0.0000064743988785127494,"timeStamp":1663795142788}},"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":19002,"cycles":6,"hz":231573.86742754732,"stats":{"moe":6.302697022140256e-8,"rme":1.459539924641105,"sem":3.215661745989927e-8,"deviation":2.55235238411793e-7,"mean":0.000004318276544363844,"variance":6.514502692712481e-14,"numSamples":63},"times":{"cycle":0.08205589089600177,"elapsed":7.139,"period":0.000004318276544363844,"timeStamp":1663795204585}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:104.0) Gecko/20100101 Firefox/104.0":{"aborted":false,"count":10891,"cycles":3,"hz":62817.67529471275,"stats":{"moe":0.0000065432085986161076,"rme":41.102915313343914,"sem":0.0000031996130066582435,"deviation":0.000017525002190336473,"mean":0.000015919086392618673,"variance":3.071257017712982e-10,"numSamples":30},"times":{"cycle":0.17337476990200998,"elapsed":5.632,"period":0.000015919086392618673,"timeStamp":1663795337070}}}},{"name":"addViaFnWithTryCatch","code":"test(addViaFnWithTryCatch);","results":{"aborted":false,"count":10744,"cycles":3,"hz":58914.85682420481,"stats":{"moe":0.0000067566274666995985,"rme":39.80657398150965,"sem":0.0000033088283382466205,"deviation":0.000018422776504411613,"mean":0.00001697364729212337,"variance":3.3939869413150064e-10,"numSamples":31},"times":{"cycle":0.18236486650657346,"elapsed":6.113,"period":0.00001697364729212337,"timeStamp":1663795342708}},"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":12142,"cycles":5,"hz":152356.06259574473,"stats":{"moe":8.587241484702703e-8,"rme":1.308318301168141,"sem":4.3812456554605624e-8,"deviation":3.5322731732936273e-7,"mean":0.000006563572088715357,"variance":1.247695377076983e-13,"numSamples":65},"times":{"cycle":0.07969489230118186,"elapsed":6.19,"period":0.000006563572088715357,"timeStamp":1663795149161}},"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":18464,"cycles":5,"hz":235578.1853446638,"stats":{"moe":3.34292496846073e-8,"rme":0.7875201978133461,"sem":1.7055739635003723e-8,"deviation":1.3644591708002978e-7,"mean":0.0000042448752143028235,"variance":1.8617488287810365e-14,"numSamples":64},"times":{"cycle":0.07837737595688733,"elapsed":6.705,"period":0.0000042448752143028235,"timeStamp":1663795211756}},"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:104.0) Gecko/20100101 Firefox/104.0":{"aborted":false,"count":10744,"cycles":3,"hz":58914.85682420481,"stats":{"moe":0.0000067566274666995985,"rme":39.80657398150965,"sem":0.0000033088283382466205,"deviation":0.000018422776504411613,"mean":0.00001697364729212337,"variance":3.3939869413150064e-10,"numSamples":31},"times":{"cycle":0.18236486650657346,"elapsed":6.113,"period":0.00001697364729212337,"timeStamp":1663795342708}}}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment