Skip to content

Instantly share code, notes, and snippets.

@JobLeonard
Last active June 27, 2024 13:30
Show Gist options
  • Save JobLeonard/88c128e7fb0ee2182db5dd50da2e366f to your computer and use it in GitHub Desktop.
Save JobLeonard/88c128e7fb0ee2182db5dd50da2e366f to your computer and use it in GitHub Desktop.
Array and object destructuring
{"title":"Array and object destructuring","initialization":"// runs once\nclass StupidIdea {\n constructor() {\n this.done = false;\n this.value = 0;\n this.i = 0;\n this.a = random();\n this.b = random();\n this.c = random();\n }\n \n next() {\n if (!(this.done = this.i > 2)) {\n switch (this.i++) {\n case 0: this.value = this.a; return this;\n case 1: this.value = this.b; return this;\n case 2: this.value = this.c; return this;\n default:\n case 3: this.value = 0; return this;\n }\n }\n }\n \n [Symbol.iterator](){\n this.i = 0;\n return this;\n }\n}\n\nfunction makeObject1() {\n return {0: random(), 1: random(), 2: random()};\n}\n\nfunction makeObject2() {\n return {a: random(), b: random(), c: random()};\n}\n\nconst arrayData = [], object1Data = [], object2Data = [], stupidData = [], {random} = Math, dataSize = 100000;\n\n\nfor (let i = 0; i < dataSize; i++) {\n arrayData[i] = [random(), random(), random()];\n object1Data[i] = makeObject1();\n object2Data[i] = makeObject2();\n stupidData[i] = new StupidIdea();\n}\n\nlet sum = 0;","setup":"// runs before each test\nsum = 0;","tests":[{"name":"array destructure","code":"for (let i = 0; i < dataSize; i++) {\n const [a, b, c] = arrayData[i];\n sum += a + b + c;\n}","results":{"aborted":false,"count":75,"cycles":2,"hz":884.0579710144924,"stats":{"moe":0.000023046194833427003,"rme":2.0374172244044155,"sem":0.000011758262670115818,"deviation":0.00009183496720849738,"mean":0.001131147540983607,"variance":8.43366120218579e-9,"numSamples":61},"times":{"cycle":0.08483606557377052,"elapsed":5.818,"period":0.001131147540983607,"timeStamp":1719494962172}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0":{"aborted":false,"count":75,"cycles":2,"hz":884.0579710144924,"stats":{"moe":0.000023046194833427003,"rme":2.0374172244044155,"sem":0.000011758262670115818,"deviation":0.00009183496720849738,"mean":0.001131147540983607,"variance":8.43366120218579e-9,"numSamples":61},"times":{"cycle":0.08483606557377052,"elapsed":5.818,"period":0.001131147540983607,"timeStamp":1719494962172}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36":{"aborted":false,"count":27,"cycles":5,"hz":314.17780953353076,"stats":{"moe":0.00007510789406199738,"rme":2.359723363507482,"sem":0.00003832035411326397,"deviation":0.00028931232905608946,"mean":0.003182910981156594,"variance":8.370162374385897e-8,"numSamples":57},"times":{"cycle":0.08593859649122804,"elapsed":6.036,"period":0.003182910981156594,"timeStamp":1719494747807}}}},{"name":"array destructure as object","code":"for (let i = 0; i < dataSize; i++) {\n const {0: a, 1: b, 2: c} = arrayData[i];\n sum += a + b + c;\n}","results":{"aborted":false,"count":118,"cycles":5,"hz":1405.1325302854164,"stats":{"moe":0.000014880655933513793,"rme":2.0909293724164932,"sem":0.000007592171394649895,"deviation":0.00005880870674606276,"mean":0.0007116766414886686,"variance":3.4584639891444077e-9,"numSamples":60},"times":{"cycle":0.08397784369566288,"elapsed":6.055,"period":0.0007116766414886686,"timeStamp":1719494967996}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0":{"aborted":false,"count":118,"cycles":5,"hz":1405.1325302854164,"stats":{"moe":0.000014880655933513793,"rme":2.0909293724164932,"sem":0.000007592171394649895,"deviation":0.00005880870674606276,"mean":0.0007116766414886686,"variance":3.4584639891444077e-9,"numSamples":60},"times":{"cycle":0.08397784369566288,"elapsed":6.055,"period":0.0007116766414886686,"timeStamp":1719494967996}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36":{"aborted":false,"count":40,"cycles":7,"hz":437.090227911333,"stats":{"moe":0.00004024909309474556,"rme":1.7592485274006797,"sem":0.00002053525157895182,"deviation":0.00015229350169195132,"mean":0.0022878571428571436,"variance":2.3193310657596382e-8,"numSamples":55},"times":{"cycle":0.09151428571428574,"elapsed":6.046,"period":0.0022878571428571436,"timeStamp":1719494753848}}}},{"name":"object destructure with number keys","code":"for (let i = 0; i < dataSize; i++) {\n const {0: a, 1: b, 2: c} = object1Data[i];\n sum += a + b + c;\n}","results":{"aborted":false,"count":34,"cycles":6,"hz":421.0841415453557,"stats":{"moe":0.00003630381142249735,"rme":1.5286959267666773,"sem":0.00001852235276658028,"deviation":0.00014701661734854186,"mean":0.0023748222773958072,"variance":2.161388577660758e-8,"numSamples":63},"times":{"cycle":0.08074395743145744,"elapsed":6.166,"period":0.0023748222773958072,"timeStamp":1719494974057}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0":{"aborted":false,"count":34,"cycles":6,"hz":421.0841415453557,"stats":{"moe":0.00003630381142249735,"rme":1.5286959267666773,"sem":0.00001852235276658028,"deviation":0.00014701661734854186,"mean":0.0023748222773958072,"variance":2.161388577660758e-8,"numSamples":63},"times":{"cycle":0.08074395743145744,"elapsed":6.166,"period":0.0023748222773958072,"timeStamp":1719494974057}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36":{"aborted":false,"count":35,"cycles":3,"hz":439.8742724265244,"stats":{"moe":0.00004882271749878113,"rme":2.147585733766209,"sem":0.000024909549744276087,"deviation":0.0001977134216818413,"mean":0.0022733768776327734,"variance":3.909059711314159e-8,"numSamples":63},"times":{"cycle":0.07956819071714707,"elapsed":5.943,"period":0.0022733768776327734,"timeStamp":1719494759899}}}},{"name":"object destructure with property names","code":"for (let i = 0; i < dataSize; i++) {\n const {a, b, c} = object2Data[i];\n sum += a + b + c;\n}","results":{"aborted":false,"count":70,"cycles":4,"hz":870.2408702408707,"stats":{"moe":0.000029062313320786343,"rme":2.5291212835493955,"sem":0.000014827710877952216,"deviation":0.00011862168702361773,"mean":0.0011491071428571423,"variance":1.407110463232912e-8,"numSamples":64},"times":{"cycle":0.08043749999999995,"elapsed":6.02,"period":0.0011491071428571423,"timeStamp":1719494980228}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0":{"aborted":false,"count":70,"cycles":4,"hz":870.2408702408707,"stats":{"moe":0.000029062313320786343,"rme":2.5291212835493955,"sem":0.000014827710877952216,"deviation":0.00011862168702361773,"mean":0.0011491071428571423,"variance":1.407110463232912e-8,"numSamples":64},"times":{"cycle":0.08043749999999995,"elapsed":6.02,"period":0.0011491071428571423,"timeStamp":1719494980228}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36":{"aborted":false,"count":44,"cycles":4,"hz":534.6758815982215,"stats":{"moe":0.00003441548940124471,"rme":1.8401132136244762,"sem":0.000017558923163900362,"deviation":0.00013713957395012263,"mean":0.0018702919552138003,"variance":1.8807262743221157e-8,"numSamples":61},"times":{"cycle":0.08229284602940722,"elapsed":5.915,"period":0.0018702919552138003,"timeStamp":1719494765847}}}},{"name":"direct indexing on array","code":"for (let i = 0; i < dataSize; i++) {\n const arr = arrayData[i];\n sum += arr[0] + arr[1] + arr[2];\n}","results":{"aborted":false,"count":136,"cycles":3,"hz":1613.7821160111073,"stats":{"moe":0.000018303932925621755,"rme":2.9538559608035255,"sem":0.000009338741288582529,"deviation":0.00007233757897043946,"mean":0.0006196623386010539,"variance":5.232725331304565e-9,"numSamples":60},"times":{"cycle":0.08427407804974332,"elapsed":5.847,"period":0.0006196623386010539,"timeStamp":1719494986253}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0":{"aborted":false,"count":136,"cycles":3,"hz":1613.7821160111073,"stats":{"moe":0.000018303932925621755,"rme":2.9538559608035255,"sem":0.000009338741288582529,"deviation":0.00007233757897043946,"mean":0.0006196623386010539,"variance":5.232725331304565e-9,"numSamples":60},"times":{"cycle":0.08427407804974332,"elapsed":5.847,"period":0.0006196623386010539,"timeStamp":1719494986253}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36":{"aborted":false,"count":35,"cycles":6,"hz":438.80597014925365,"stats":{"moe":0.000028971373833356843,"rme":1.271281180150285,"sem":0.000014781313180284104,"deviation":0.00011732303617797895,"mean":0.0022789115646258506,"variance":1.3764694818019359e-8,"numSamples":63},"times":{"cycle":0.07976190476190477,"elapsed":6.005,"period":0.0022789115646258506,"timeStamp":1719494771768}}}},{"name":"direct number indexing on object","code":"for (let i = 0; i < dataSize; i++) {\n const obj = object1Data[i];\n sum += obj[0] + obj[1] + obj[2];\n}","results":{"aborted":false,"count":39,"cycles":4,"hz":490.4327780625124,"stats":{"moe":0.000031139376890766806,"rme":1.5271771115674366,"sem":0.000015887437189166737,"deviation":0.0001261026233180827,"mean":0.002039015426233473,"variance":1.5901871607702257e-8,"numSamples":63},"times":{"cycle":0.07952160162310544,"elapsed":6.016,"period":0.002039015426233473,"timeStamp":1719494992106}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0":{"aborted":false,"count":39,"cycles":4,"hz":490.4327780625124,"stats":{"moe":0.000031139376890766806,"rme":1.5271771115674366,"sem":0.000015887437189166737,"deviation":0.0001261026233180827,"mean":0.002039015426233473,"variance":1.5901871607702257e-8,"numSamples":63},"times":{"cycle":0.07952160162310544,"elapsed":6.016,"period":0.002039015426233473,"timeStamp":1719494992106}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36":{"aborted":false,"count":36,"cycles":3,"hz":459.0477842544656,"stats":{"moe":0.00003746720707325752,"rme":1.7199238389182105,"sem":0.000019115921976151795,"deviation":0.00015411749006810033,"mean":0.0021784224525211223,"variance":2.3752200744891002e-8,"numSamples":65},"times":{"cycle":0.07842320829076041,"elapsed":6.064,"period":0.0021784224525211223,"timeStamp":1719494777778}}}},{"name":"direct property access on object","code":"for (let i = 0; i < dataSize; i++) {\n const obj = object2Data[i];\n sum += obj.a + obj.b + obj.c;\n}","results":{"aborted":false,"count":80,"cycles":4,"hz":1000.7069218692693,"stats":{"moe":0.000026116017853537034,"rme":2.6134479837695923,"sem":0.000013324498904865833,"deviation":0.00010659599123892667,"mean":0.0009992935775162335,"variance":1.136270534820933e-8,"numSamples":64},"times":{"cycle":0.07994348620129868,"elapsed":6.058,"period":0.0009992935775162335,"timeStamp":1719494998127}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0":{"aborted":false,"count":80,"cycles":4,"hz":1000.7069218692693,"stats":{"moe":0.000026116017853537034,"rme":2.6134479837695923,"sem":0.000013324498904865833,"deviation":0.00010659599123892667,"mean":0.0009992935775162335,"variance":1.136270534820933e-8,"numSamples":64},"times":{"cycle":0.07994348620129868,"elapsed":6.058,"period":0.0009992935775162335,"timeStamp":1719494998127}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36":{"aborted":false,"count":41,"cycles":6,"hz":511.3838843793305,"stats":{"moe":0.000042664006841060104,"rme":2.1817685541567644,"sem":0.0000217673504291123,"deviation":0.00017277298780867873,"mean":0.0019554781262098348,"variance":2.9850505316337846e-8,"numSamples":63},"times":{"cycle":0.08017460317460323,"elapsed":6.02,"period":0.0019554781262098348,"timeStamp":1719494783847}}}},{"name":"Class with custom iterator, array destructured","code":"for (let i = 0; i < dataSize; i++) {\n const [a, b, c] = stupidData[i];\n sum += a + b + c;\n}","results":{"aborted":false,"count":17,"cycles":3,"hz":206.22129798111075,"stats":{"moe":0.0001290470806470169,"rme":2.66122564717009,"sem":0.00006584034726888617,"deviation":0.0005099971369618182,"mean":0.004849159663865548,"variance":2.600970797092515e-7,"numSamples":60},"times":{"cycle":0.08243571428571432,"elapsed":6.034,"period":0.004849159663865548,"timeStamp":1719495004191}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0":{"aborted":false,"count":17,"cycles":3,"hz":206.22129798111075,"stats":{"moe":0.0001290470806470169,"rme":2.66122564717009,"sem":0.00006584034726888617,"deviation":0.0005099971369618182,"mean":0.004849159663865548,"variance":2.600970797092515e-7,"numSamples":60},"times":{"cycle":0.08243571428571432,"elapsed":6.034,"period":0.004849159663865548,"timeStamp":1719495004191}},"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36":{"aborted":false,"count":26,"cycles":4,"hz":317.21523349991395,"stats":{"moe":0.000075935065075744,"rme":2.4087759398833293,"sem":0.00003874238014068572,"deviation":0.0002975858684799373,"mean":0.003152433724467622,"variance":8.855734911895851e-8,"numSamples":59},"times":{"cycle":0.08196327683615817,"elapsed":6.041,"period":0.003152433724467622,"timeStamp":1719494789872}}}},{"name":"Class with custom iterator, object destructured","code":"for (let i = 0; i < dataSize; i++) {\n const {a, b, c} = stupidData[i];\n sum += a + b + c;\n}","results":{"aborted":false,"count":93,"cycles":4,"hz":1156.9789094590196,"stats":{"moe":0.000024974046088933575,"rme":2.8894444608753664,"sem":0.000012741860249455905,"deviation":0.00010113538038119926,"mean":0.0008643199904720649,"variance":1.0228365164849864e-8,"numSamples":63},"times":{"cycle":0.08038175911390204,"elapsed":5.997,"period":0.0008643199904720649,"timeStamp":1719495010230}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36":{"aborted":false,"count":66,"cycles":4,"hz":821.6388478841789,"stats":{"moe":0.000032564628764506026,"rme":2.6756364059844726,"sem":0.000016614606512503073,"deviation":0.00013187435088983187,"mean":0.0012170797456511739,"variance":1.7390844422614503e-8,"numSamples":63},"times":{"cycle":0.08032726321297748,"elapsed":6.002,"period":0.0012170797456511739,"timeStamp":1719494795918}},"Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0":{"aborted":false,"count":93,"cycles":4,"hz":1156.9789094590196,"stats":{"moe":0.000024974046088933575,"rme":2.8894444608753664,"sem":0.000012741860249455905,"deviation":0.00010113538038119926,"mean":0.0008643199904720649,"variance":1.0228365164849864e-8,"numSamples":63},"times":{"cycle":0.08038175911390204,"elapsed":5.997,"period":0.0008643199904720649,"timeStamp":1719495010230}}}},{"name":"Class with custom iterator, direct property access","code":"for (let i = 0; i < dataSize; i++) {\n const obj = stupidData[i];\n sum += obj.a + obj.b + obj.c;\n}","results":{"aborted":false,"count":103,"cycles":4,"hz":1246.7982263465694,"stats":{"moe":0.000023345010490561905,"rme":2.9106517673674643,"sem":0.000011910719638041789,"deviation":0.00009378510021508815,"mean":0.0008020543973103411,"variance":8.795645022354128e-9,"numSamples":62},"times":{"cycle":0.08261160292296513,"elapsed":6.066,"period":0.0008020543973103411,"timeStamp":1719495016232}},"platforms":{"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36":{"aborted":false,"count":66,"cycles":4,"hz":823.371577251265,"stats":{"moe":0.00003236561843589694,"rme":2.6648930300277085,"sem":0.000016513070630559665,"deviation":0.00013210456504447732,"mean":0.0012145184842770375,"variance":1.745161610559054e-8,"numSamples":64},"times":{"cycle":0.08015821996228448,"elapsed":6.103,"period":0.0012145184842770375,"timeStamp":1719494801925}},"Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0":{"aborted":false,"count":103,"cycles":4,"hz":1246.7982263465694,"stats":{"moe":0.000023345010490561905,"rme":2.9106517673674643,"sem":0.000011910719638041789,"deviation":0.00009378510021508815,"mean":0.0008020543973103411,"variance":8.795645022354128e-9,"numSamples":62},"times":{"cycle":0.08261160292296513,"elapsed":6.066,"period":0.0008020543973103411,"timeStamp":1719495016232}}}}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment