最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

optimization - Two-letter Variable Names in Javascript? - Stack Overflow

programmeradmin5浏览0评论

I was looking at an answer to an SO question today where the variable names are ua, rv, etc. And I thought, "Man, when will people learn to use full-size variable names, memory is not a problem any more" but then, it is Javascript so it has to e across the wire and perhaps long variable names even slow down interpretation.

Is using short variable names in Javascript premature optimization?

While I'm here, are there any libraries for Rails or PHP that will press Javscript on the fly, so I can keep my Javascript with long names on the server?

I was looking at an answer to an SO question today where the variable names are ua, rv, etc. And I thought, "Man, when will people learn to use full-size variable names, memory is not a problem any more" but then, it is Javascript so it has to e across the wire and perhaps long variable names even slow down interpretation.

Is using short variable names in Javascript premature optimization?

While I'm here, are there any libraries for Rails or PHP that will press Javscript on the fly, so I can keep my Javascript with long names on the server?

Share Improve this question edited May 23, 2017 at 11:59 CommunityBot 11 silver badge asked May 28, 2009 at 0:31 Dan RosenstarkDan Rosenstark 69.8k60 gold badges291 silver badges424 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 12

The only reason to use short variable names in JS is to save bytes over the wire. However, developing like that is ridiculous. Do they write JS without whitespace, too? There are tools which optimize finished JS. Crockford's is one of the most popular (though it does not shorten variable names). I can't recall offhand one that does obfuscate/shorten variable names, but they do exist and it's not that hard to write one, either. Google Closure is a very impressive JavaScript piler that turns this:

var myFunction = function(arg1, arg2) {
    var foo = getValue(arg2);
    for(var count = 0; count < arg1.length; count++) {
        alert(foo);
    }
};

into this:

function a(b,c){var d=e(c);for(var f=0;f<b.length;f++){alert(d)}}

Dont use short variable names for optimization, during development. That would severely decrease readability. Compress your JS/CSS files at pile/deploy time, using something like YUI Compressor.

People use short variable names in javascript purely to save on bandwidth. It does not affect execution speed of the javascript. And I don't know about rails or PHP libraries, but there are certainly tools out there that can press your javascript files (by renaming variables to be shorter and removing unnecessary whitespace).

We have not any reason to use not readable code at development.

As the other answers, I think you have a lot of resources to save bandwith and make happy the user with a fast load of the page.

Check these articles:

close-look-into-include-javascript-pression

Production-Grade-JS

I normal development, most of these answers are correct. There is no reason to use non-descriptive variable names.

However, when writing answers and examples on SO, variables don't necessarily mean anything in particular. They're just there for demonstration purposes, and have no need for any semantic meaning.

发布评论

评论列表(0)

  1. 暂无评论