Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
Is it a good practice to rename global variables and methods for minification

Let's say I have used document and setAttribute several times throughout the code.

Now, if I declare these in the beginning,

const _document     = document,
      _setAttribute = 'setAttribute';

and then later in the script, use them as,

_document.querySelector('.class')[_setAttribute]('data-something', '');

will it be fine, or will it have some flaws?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

IMO saving a couple bytes shouldn't really matter to you... Today's internet access is very fast and there are so many tools that can do it for you after you code, you are only slowing your development efforts by doing this. Code will be harder to read, harder to maintain. Also between _document and document you are using up more characters than the original variable... To what use?

Also note that your file can be served gzipped which will have a great impact on your file size.

Unless you are using a good http/2 server, https negotiation will likely take up a good chunk of the loading time for a medium sized file. I timed my bank's website, to serve a 44KB javascript file, it took 347ms*. From these 347ms, it took only 117 to download the file. The rest was mostly finding the server (DNS) and negotiating the connection (SSL). Shaving 50% off of the file size would hypothetically remove 59ms of download time. Putting the request length at 288 an effective 17% faster request.

enter image description here

All in all, this kind of optimization will certainly hurt your development efforts. Since there are already great tools to minimize at the end, there is no immediate need to harm your productivity by doing it yourself. Rules in optimizations are measure, measure, measure. So once your application/code is done, test its download speed, and performance. Then see which one you want to optimize and focus there.

*I'm in the train on my PC using internet that my phone shares through bluetooth.

[EDIT] Also, if you want to keep maintainability and reduce code size, you could create utility function, like if your example is used quite a lot it might help.

function setAttributeSelector(selector,attribute,value){
    document.querySelector('.class').setAttribute('data-something', '');
}

//and use it
setAttributeSelector('.class','data-something','');

But do this only if it makes sense to your codebase.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!