You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
'use strict';constbenchmark=require('benchmark');classFoo{}constclients=[];for(leti=0;i<1000;i++){clients.push(newFoo());}functionshuffle(arr){leti,j,x;for(i=arr.length-1;i>0;i--){j=Math.floor(Math.random()*(i+1));x=arr[i];arr[i]=arr[j];arr[j]=x;}}functionremove(element){// To remove the element the corresponding index must be found first.for(leti=0;i<array.length;i++){if(array[i]===element){array.splice(i,1);break;}}}constarray=[];constset=newSet();constsuite=newbenchmark.Suite();suite.add('array',function(){leti;for(i=0;i<clients.length;i++){array.push(clients[i]);}shuffle(clients);for(i=0;i<clients.length;i++){remove(clients[i]);}});suite.add('set',function(){leti;for(i=0;i<clients.length;i++){set.add(clients[i]);}shuffle(clients);for(i=0;i<clients.length;i++){set.delete(clients[i]);}});suite.on('cycle',function(event){console.log(event.target.toString());});suite.on('complete',function(){console.log('Fastest is '+this.filter('fastest').map('name'));});suite.run({async: true});
$ node test.js
array x 3,413 ops/sec ±1.71% (85 runs sampled)
set x 9,977 ops/sec ±4.04% (73 runs sampled)
Fastest is set
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
test.js
Source: websockets#2044 (comment)
Beta Was this translation helpful? Give feedback.
All reactions