r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

Show parent comments

1

u/barrtender Mar 19 '19 edited Mar 20 '19

I remembered!

Copy/paste this into your browser console:

function listGen() {
 return [1,2,3,4,5,6,7,8,9,10];
}
var aggregator = {};
 for(var l=0;l<listGen().length;l++){
   aggregator[`${l}`] = {};
 }
for (var i=0;i<1000;i++) {
 var list = listGen();
 list.sort(() => 2*(Math.random() - 0.5));
 for(var l=0;l<list.length;l++){
   aggregator[`${l}`][`${list[l]}`] = (aggregator[`${l}`][`${list[l]}`] || 0) + 1;
 }
}
aggregator

It runs that sort on a previously sorted list 1000 times and aggregates for each index how often a given number was in that spot. This'll give you an idea of the distribution. Ideally every index has each number roughly 100 times.

I just ran it and got:

0: {1: 199, 2: 67, 3: 111, 4: 133, 5: 65, 6: 77, 7: 106, 8: 118, 9: 62, 10: 62}
1: {1: 130, 2: 143, 3: 91, 4: 136, 5: 86, 6: 84, 7: 92, 8: 117, 9: 57, 10: 64}
2: {1: 82, 2: 129, 3: 92, 4: 100, 5: 81, 6: 106, 7: 89, 8: 93, 9: 89, 10: 139}
3: {1: 88, 2: 138, 3: 103, 4: 97, 5: 62, 6: 100, 7: 91, 8: 98, 9: 92, 10: 131}
4: {1: 77, 2: 119, 3: 102, 4: 93, 5: 117, 6: 103, 7: 83, 8: 94, 9: 103, 10: 109}
5: {1: 66, 2: 99, 3: 110, 4: 83, 5: 142, 6: 111, 7: 87, 8: 96, 9: 138, 10: 68}
6: {1: 92, 2: 105, 3: 114, 4: 119, 5: 125, 6: 80, 7: 78, 8: 104, 9: 128, 10: 55}
7: {1: 83, 2: 102, 3: 104, 4: 104, 5: 119, 6: 92, 7: 79, 8: 100, 9: 100, 10: 117}
8: {1: 87, 2: 73, 3: 93, 4: 79, 5: 107, 6: 112, 7: 124, 8: 84, 9: 131, 10: 110}
9: {1: 96, 2: 25, 3: 80, 4: 56, 5: 96, 6: 135, 7: 171, 8: 96, 9: 100, 10: 145}

As we can see 1 ended up in the first position 199 times out of 1000. There are some worryingly low numbers in there too. Maybe our sample size just isn't big enough. Luckily this is fast, so let's do 10,000:

0: {1: 2002, 2: 726, 3: 997, 4: 1283, 5: 814, 6: 892, 7: 950, 8: 1144, 9: 562, 10: 630}
1: {1: 1353, 2: 1408, 3: 952, 4: 1242, 5: 827, 6: 885, 7: 998, 8: 1073, 9: 602, 10: 660}
2: {1: 925, 2: 1326, 3: 980, 4: 945, 5: 791, 6: 1034, 7: 854, 8: 975, 9: 927, 10: 1243}
3: {1: 706, 2: 1241, 3: 1035, 4: 1028, 5: 790, 6: 1069, 7: 790, 8: 928, 9: 1129, 10: 1284}
4: {1: 661, 2: 1234, 3: 1044, 4: 955, 5: 1032, 6: 985, 7: 832, 8: 925, 9: 1080, 10: 1252}
5: {1: 747, 2: 1198, 3: 1167, 4: 1051, 5: 1240, 6: 945, 7: 876, 8: 1028, 9: 1136, 10: 612}
6: {1: 801, 2: 1074, 3: 1152, 4: 1042, 5: 1249, 6: 880, 7: 911, 8: 1053, 9: 1209, 10: 629}
7: {1: 838, 2: 859, 3: 1022, 4: 916, 5: 1144, 6: 961, 7: 929, 8: 960, 9: 1118, 10: 1253}
8: {1: 973, 2: 663, 3: 871, 4: 854, 5: 1128, 6: 1065, 7: 1153, 8: 933, 9: 1116, 10: 1244}
9: {1: 994, 2: 271, 3: 780, 4: 684, 5: 985, 6: 1284, 7: 1707, 8: 981, 9: 1121, 10: 1193}

Nope, we see the same distribution. 1 appears first far more often than 4 (20% vs 7%), for example.

I encourage you to try it yourself.

Now the question is "why". This has to do with the sorting algorithm that's being used.

https://v8.dev/blog/array-sort https://bugs.chromium.org/p/v8/issues/detail?id=90

I gotta go, but those are good starting places for learning why.

Edit: I got a second to come back to this, so what I ended up doing when this came up at work was something like this:

function listGen() {
 return [1,2,3,4,5,6,7,8,9,10];
}
function sortingFn2(list) {
  var retval = [];
  while (list.length > 0) {
	retval.push(list.splice(Math.random() * list.length, 1));
  }
  return retval;
}
var aggregator = {};
 for(var l=0;l<listGen().length;l++){
   aggregator[`${l}`] = {};
 }
for (var i=0;i<1000;i++) {
 var list = listGen();
 list = sortingFn2(list);
 for(var l=0;l<list.length;l++){
   aggregator[`${l}`][`${list[l]}`] = (aggregator[`${l}`][`${list[l]}`] || 0) + 1;
 }
}
aggregator

Which ends up fairly uniform:

0: {1: 105, 2: 85, 3: 100, 4: 100, 5: 102, 6: 114, 7: 116, 8: 101, 9: 89, 10: 88}
1: {1: 100, 2: 88, 3: 98, 4: 96, 5: 92, 6: 94, 7: 121, 8: 105, 9: 95, 10: 111}
2: {1: 86, 2: 106, 3: 94, 4: 105, 5: 98, 6: 95, 7: 110, 8: 106, 9: 101, 10: 99}
3: {1: 104, 2: 96, 3: 87, 4: 86, 5: 108, 6: 113, 7: 122, 8: 76, 9: 115, 10: 93}
4: {1: 108, 2: 106, 3: 115, 4: 91, 5: 87, 6: 83, 7: 91, 8: 99, 9: 83, 10: 137}
5: {1: 95, 2: 98, 3: 97, 4: 104, 5: 101, 6: 99, 7: 83, 8: 126, 9: 92, 10: 105}
6: {1: 105, 2: 94, 3: 103, 4: 109, 5: 101, 6: 98, 7: 97, 8: 93, 9: 113, 10: 87}
7: {1: 105, 2: 107, 3: 107, 4: 96, 5: 110, 6: 112, 7: 79, 8: 105, 9: 97, 10: 82}
8: {1: 112, 2: 113, 3: 97, 4: 108, 5: 104, 6: 85, 7: 86, 8: 97, 9: 102, 10: 96}
9: {1: 80, 2: 107, 3: 102, 4: 105, 5: 97, 6: 107, 7: 95, 8: 92, 9: 113, 10: 102}

Edit 2: Because my kid won't fall asleep I decided to run the first code in Node in an online repl that I found (it doesn't work well on the phone). Distribution was even worse:

{ 
'0': { '1': 297, '2': 291, '3': 199, '4': 105, '5': 48, '6': 29, '7': 17, '8': 8, '9': 3, '10': 3 },
 '1': { '1': 267, '2': 304, '3': 192, '4': 106, '5': 62, '6': 39, '7': 21, '8': 5, '9': 1, '10': 3 }, 
'2': { '1': 197, '2': 170, '3': 255, '4': 185, '5': 102, '6': 50, '7': 14, '8': 16, '9': 4, '10': 7 }, 
'3': { '1': 107, '2': 116, '3': 161, '4': 234, '5': 157, '6': 111, '7': 58, '8': 33, '9': 17, '10': 6 },
 '4': { '1': 66, '2': 66, '3': 87, '4': 161, '5': 241, '6': 167, '7': 105, '8': 57, '9': 34, '10': 16 }, 
'5': { '1': 30, '2': 31, '3': 56, '4': 101, '5': 168, '6': 250, '7': 167, '8': 110, '9': 53, '10': 34 }, 
'6': { '1': 17, '2': 12, '3': 27, '4': 45, '5': 111, '6': 161, '7': 239, '8': 221, '9': 112, '10': 55 }, 
'7': { '1': 11, '2': 4, '3': 8, '4': 39, '5': 68, '6': 107, '7': 189, '8': 238, '9': 216, '10': 120 }, 
'8': { '1': 7, '2': 3, '3': 12, '4': 13, '5': 31, '6': 59, '7': 123, '8': 186, '9': 314, '10': 252 }, 
'9': { '1': 1, '2': 3, '3': 3, '4': 11, '5': 12, '6': 27, '7': 67, '8': 126, '9': 246, '10': 504 } } 

Which is horrible formatted, because again I'm on my phone. But the point is that it barely sorted the list!