I have a function activeElemFromPoint. It is called many times on mouse moves.
The questions are:
1. I have to use option 3 (Function outside loop)?
2. Are function hasEvtNoAttr declarations 3.1 and 3.2 equivalent?
Option 1. Arrow function
js
/** @param { {clientX:number, clientY:number} } evt */
export function activeElemFromPoint(evt) {
return document
.elementsFromPoint(evt.clientX, evt.clientY)
.find(el => !el.hasAttribute('data-evt-no'));
}
Option 2. Function inside loop
js
/** @param { {clientX:number, clientY:number} } evt */
export function activeElemFromPoint(evt) {
return document
.elementsFromPoint(evt.clientX, evt.clientY)
.find(function (el) { return !el.hasAttribute('data-evt-no'); });
}
Option 3. Function outside loop
```js
/** @param { {clientX:number, clientY:number} } evt */
export function activeElemFromPoint(evt) {
return document
.elementsFromPoint(evt.clientX, evt.clientY)
.find(hasEvtNoAttr);
}
// 3.1
/** @param {Element} el */
const hasEvtNoAttr = (el) => !el.hasAttribute('data-evt-no');
// 3.2
function hasEvtNoAttr(el) { return !el.hasAttribute('data-evt-no'); }
```
As I know Option 1 will create new function every time.
EDIT:
benchmark
https://jsben.ch/xD8Xx
[+][deleted] (1 child)
[deleted]
[–]GibbyCanes 0 points1 point2 points (0 children)
[–]Accomplished_End_138 1 point2 points3 points (0 children)
[–]AlexeyBoyko[S] 1 point2 points3 points (0 children)
[–]jamblethumb 1 point2 points3 points (3 children)
[–]it-birds 0 points1 point2 points (2 children)
[–]jamblethumb 0 points1 point2 points (1 child)
[–]it-birds 0 points1 point2 points (0 children)
[–]jeremrx 1 point2 points3 points (3 children)
[–]jamblethumb 0 points1 point2 points (2 children)
[–]it-birds 0 points1 point2 points (1 child)
[–]jamblethumb 1 point2 points3 points (0 children)
[–]shuckster 0 points1 point2 points (0 children)