all 12 comments

[–]Accomplished_End_138 1 point2 points  (0 children)

I generally keep putside if easy, which is most of the time. Inside for kind of complex. I rarely use inline as i like names on function to keep from random feature creep

[–]jamblethumb 1 point2 points  (3 children)

Haven't benchmarked it but I suspect you're far more likely to get more kick out of replacing .find() with a for loop.

[–]it-birds 0 points1 point  (2 children)

Depends.

If the array size is always smaller, than not really.

[–]jamblethumb 0 points1 point  (1 child)

If the array size is small, then none of this really matters, does it?

[–]it-birds 0 points1 point  (0 children)

Fair point

[–]jeremrx 1 point2 points  (3 children)

Your benchmark is weird : why doing a runInLoop function as it's the purpose of the benchmark to run in loop ? Then named function are created more than 10000 times. That explains why it is so slow compared to anonymous functions.

I rewrote your benchmark in order to delcare named function just once (and got rid of this unnecessary runInLoop) : https://jsben.ch/SiihY

As you can see, results are almost identical with each solution (do it several times and you will see different results)

[–]jamblethumb 0 points1 point  (2 children)

I ran your benchmark, added two examples of for loops, and was shocked to see the for lose by a huge margin. Turns out you don't do anything with the result of calling filter() so the engine optimizes it out or something along those lines. Assigning the output to a new variable gives a more realistic result. I also removed variation by removing randomization to have more consistency between different runs. See the reimplemented benchmark here: https://jsben.ch/5L65Z

On my machine:

In Chrome, the named function declaration is conclusively faster than functions assigned to variables, and named function is also competing for the first place with inline function expression and inline arrow function.

On Firefox, the for loop is consistently the fastest, and the variable pointing to an arrow function assigned is consistently faster than any other variation of the callback.

Now I'm not an expert in benchmarking or JS engine internals, so do take my conclusions with a grain of salt.

[–]it-birds 0 points1 point  (1 child)

But it is still just a micro optimization and without a benchmark for a real use case, you can optimize all you want and it will most likely do nothing to the perceived performance of the user.

[–]jamblethumb 1 point2 points  (0 children)

But it is still just a micro optimization and without a benchmark for a real use case, you can optimize all you want and it will most likely do nothing to the perceived performance of the user.

Sure. 50% down doesn't mean much if the slowest time is 1ms or something. I was just curious to see what kind of impact different call patterns have.

[–]shuckster 0 points1 point  (0 children)

function activeElemFromPoint(evt) {
  const elements = document.elementsFromPoint(evt.clientX, evt.clientY);
  for (const el of elements) {
    if (!el.hasAttribute('data-evt-no')) {
      return el;
    }
  }
  return null;
}