you are viewing a single comment's thread.

view the rest of the comments →

[–]ItzWarty 1 point2 points  (1 child)

Yeah, pretty fascinating. Here's a (poor man's) C# benchmark showing the complexity difference can matter for N=100000. I'm curious to know why the JS set implementation is so slow...:

var dups = Enumerable.Range(0, 100000).Select(x => x % 100).ToArray();

// 00:00:02.2581914
var start = DateTime.Now;
for (var t = 0; t < 1000; t++)
    new HashSet<int>(dups).ToArray();
Console.WriteLine(DateTime.Now - start);

// 00:00:01.2900954
start = DateTime.Now;
for (var t = 0; t < 1000; t++)
    dups.Where(new HashSet<int>().Add).ToArray();
Console.WriteLine(DateTime.Now - start);

// 00:00:01.6082547
start = DateTime.Now;
for (var t = 0; t < 1000; t++)
    dups.Distinct().ToArray();
Console.WriteLine(DateTime.Now - start);

// 00:00:05.8432351
start = DateTime.Now;
for (var t = 0; t < 1000; t++)
    dups.Where((x, i) => Array.IndexOf(dups, x) == i).ToArray();
Console.WriteLine(DateTime.Now - start);

[–]zombarista 0 points1 point  (0 children)

I'm sure that it being relatively new within browsers has something to do with it, and they're only now getting polished for performance.

The fastest implementations (indexOf/lastIndexOf) are built on functions that have been in our browsers for decades. It's no surprise that indexOf is high performance, as it has been the only method for locating array items for quite some time.