When using JavaScript, selecting elements from the DOM (Document Object Model) is a common task.
As is selection of an item from within a list, often only after checking if that item meets certain condition(s).
There are several ways to achieve this, the performance of selecting elements can have a significant impact on your application’s speed and responsiveness.
Here are some optimized methods for selecting elements in JavaScript:
Selection
getElementById: This method is very fast as it directly accesses an element by its unique ID.
const element = document.getElementById("elementId");
querySelector: This method allows you to use CSS selector syntax to target elements. It returns the first matching element.
const element = document.querySelector(".className");
///* let obj = buttons.find(t => t.id === e); if (obj) { obj.checked = !obj.checked; console.log(obj); } */
setDisplay(e.map(button => (button[id] === e ? {checked: true} : button)));
Standard built-in objects – Keyed collections
Standard built-in Array object methods
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
Standard built-in Object object methods
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
Standard built-in Number object methods
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
Standard built-in String object methods
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
Standard built-in String object methods
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON