简体   繁体   English

是否值得优化用常量替换字符串的Javascript代码?

[英]is it worth to optimize Javascript code replacing strings with constants?

I often works with jquery and sometimes my code contains lot of repeated css class names, like: 我经常使用jquery,有时候我的代码包含很多重复的css类名,比如:

$('#div1').addClass('funny-class');
...
$('#div2').addClass('funny-class');
...
$('#div1').removeClass('funny-class');

etc... I was asking to myself if is it worth to clean the code using pseudo constants like: 等...我问自己是否值得使用伪常量清理代码,如:

var constants = {
    FUNNY: 'funny-class'
}
...
$('#div1').addClass(cnst.FUNNY);
$('#div2').addClass(cnst.FUNNY);
$('#div1').removeClass(cnst.FUNNY);

First: minimization. 第一:最小化。 If "constants" is a local var (hidden into a scope) probably the minimizer could replace the occurrence of "constants.FUNNY" with something minimized like "aB". 如果“常量”是局部变量(隐藏在范围内),则最小化器可能会将“constants.FUNNY”的出现替换为像“aB”这样最小化的东西。

Second: speed. 第二:速度。 The code with "constants" is faster than the first one? 带有“常量”的代码比第一个快吗?

What do you think? 你怎么看?

You can use Google's closure compiler with advanced optimization to identify commonly repeated strings and replace them by constant variable references. 您可以使用Google的闭包编译器和高级优化来识别通常重复的字符串,并用常量变量引用替换它们。 But this optimization is marginal; 但这种优化是微不足道的; if you want to improve your code you'd get a better improvement by caching jQuery objects (this is a matter usually overlooked by many programmers): 如果你想改进你的代码,你可以通过缓存jQuery对象获得更好的改进(这是许多程序员经常忽略的事情):

var $div1 = $('#div1');
var $div2 = $('#div2');
$div1.addClass('funny-class');
$div2.addClass('funny-class');
$div1.removeClass('funny-class');

它可能不像性能那样明显,但使用常量而不是字符串总是一个好习惯,因为您可以通过更改常量的值来轻松修改值。

  1. Yes, a minifier will probably reduce the constants.FUNNY variable, where-as it probably won't detect the re-use of 'funny-class' and assign it to a variable. 是的,minifier可能会减少constants.FUNNY变量,因为它可能无法检测到'funny-class'的重复使用并将其分配给变量。

  2. The speed difference will be so marginal you shouldn't care. 速度差异很小,你不应该在意。 On one side you have to resolve the variable in the scope chain (very quick), but in the other you have to create a String primitive (also quick). 一方面,你必须解决范围链中的变量(非常快),但另一方面你必须创建一个String原语(也很快)。

The benefits of having your constants object is if you decide to change the name of the class you assign, you only have to do it in one place, not 3; 拥有constants对象的好处是,如果您决定更改所分配的类的名称,则只需在一个地方执行,而不是3; this is the sole benefit you should be considering... not a 0.0000000000000001 second speed difference. 这是你应该考虑的唯一好处......不是0.0000000000000001秒速差。

Putting it in variables can provide you with a certain amount of "central control" rather than performance. 将它放在变量中可以为您提供一定量的“中央控制”而不是性能。

However, putting them deeply in an object will incur a performance penalty. 但是,将它们深深地放在一个物体中会导致性能下降。 Keep them near the surface as possible to avoid the overhead in scope resolution. 尽可能将它们靠近表面,以避免范围分辨率的开销。 (it's minimal, but still an overhead) (这是最小的,但仍然是开销)

//this one is so deep:
constants.foo.bar.baz.bam

//this is just 1 level deep:
constants.bam

Also, I'd worry more about the jQuery calls you are making 另外,我更担心你正在制作的jQuery调用

//two calls to #div1!
$('#div1').addClass(cnst.FUNNY);       //$() for div1
$('#div2').addClass(cnst.FUNNY);
$('#div1').removeClass(cnst.FUNNY);    //another $() for div1

//do this instead
var div1 = $('#div1')             //reference div1 once in a local variable

div1.addClass(cnst.FUNNY);        //use the reference to the object
$('#div2').addClass(cnst.FUNNY);
div1.removeClass(cnst.FUNNY);     //use the same reference to the object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM