0

I'm looking for some ways to optimize my projects, both in performance and in practicality and speed, and I came across the following question, what is the most appropriate way to delegate some values ​​without losing too much performance? using function? objects?

e.g:

Objects

var z = {
  fec: 'firstElementChild',
  nx: 'nextElementSibling',
  pr: 'previuousElementSibling',
  cl: 'className',
  ci: 'clientInformation',
  qs: 'querySelector',
  qsa: 'querySelectorAll'
}

document[z.qs]('h3');

Functions

function qs(el) {
  return document.querySelector(el);
}
qs('p');
  • 1
    What is your motivation for doing this? I would argue that this additional dependency makes your code less practical, portable, performant and readable than simply calling these functions directly. – Đinh Carabus Oct 24 '21 at 16:48
  • see: [Making a short alias for document.querySelectorAll](https://stackoverflow.com/questions/13383886/making-a-short-alias-for-document-queryselectorall) – pilchard Oct 24 '21 at 16:53
  • @ĐinhCarabus I do not intend to develop applications for third parties to understand, if I am going to develop applications that anyone can read easily, I just need to write a code that is easy to read, in terms of readability, my doubt and in terms of performance, as far as I can performance would be minimal using the way i did, however i want to see opinions that differ from mine anyway thank you – Gabriel Queiroz Oct 24 '21 at 17:04
  • @GabrielQueiroz, I think the absolute fastest way would be to use global variables as described in @pilchard's link: `const qs = document.querySelector.bind(document);` no object lookup and no additional function call needed. – Đinh Carabus Oct 24 '21 at 17:10
  • @ĐinhCarabus I understand, well, thank you – Gabriel Queiroz Oct 24 '21 at 17:22

0 Answers0