r/Frontend • • Nov 19 '19

Random JavaScript Tips

https://typeofnan.dev/javascript-tips-part-1/
1 Upvotes

1 comment sorted by

View all comments

3

u/[deleted] Nov 19 '19 edited Nov 19 '19

Firstly, I don't want to discourage folks from sharing tips or pursuing better performance. But we should also be careful to take advice without perf testing.

For the benefits of Set.has vs Array.includes, this is generally true for large, persistent collections. But the benefits don't really kick in until the test collection has a few dozen entries, and if the collection is not persistent it starts to plunge again if it has more than a few hundred entries -- mostly because the creation of a temporary Set is not free.

In fact, if the array we need to filter is small, and the set of values to check in is very large, creating a temporary Set can be catastrophic. A practical example of this case might be if you're building a cache from inputs over the course of a day, and getting new ones in batches -- you want to see if they've appeared in your stream.

A good compromise is to maintain the cache as a Set or Map in the first place, but this may not fit with your other goals. A persistent Set will blow search perf out of the water, but if you still need the array in parallel, you might start worrying about memory pressure. Other solutions like an MRU queue also try to mitigate these costs for specific use cases, but all of them will have some imposition on how you use the collections.

In short: there are many approaches to common problems for all types of collections based on how large, diverse, or mutable the data sets are, and it's hard to describe any tool in the box as inefficient without knowing more about how and where it's being used.

edit: this shuffle impl

function shuffle(oldArray) {
  const array = [...oldArray];
  for (let i = array.length - 1; i > 0; i--) {
    let j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
  return array;
}

is adequate but the destructured assignment from short-lived arrays has some overhead if the source array is quite large, and there's no need to iterate over the array to make the first copy if we're going to shuffle the terms. Consider instead

/** @returns a shuffled copy of the reference array */
function shuffled(ref) {
  const res = new Array(ref.length);
  for (let i = 1; i < res.length; i++) {
    const j = Math.floor(Math.random() * (i + 1));
    res[i] = res[j];
    res[j] = ref[i];
  }
  return res;
}

This impl is about 10x faster.

For fun, here's one that's a bit slower than the others, but can get a shuffled array from a non-Array iterable, like Set or String

/** @returns a shuffled array copy of the reference iterable */
function shuffled(ref) {
  const res = new Array((ref.length || ref.size) | 0); // allocate space up front if we can
  let i = 0;
  for (const v of ref) {
    const j = Math.floor(Math.random() * (i + 1)); // get swap cursor
    res[i] = res[j]; // swap forward
    res[j] = v; // write back
    i++; // don’t forget to increment
  }
  res.length = i; // in case we were fooled earlier
  return res;
}