When should I use double or single quotes in JavaScript?

前端 未结 30 3431
攒了一身酷
攒了一身酷 2020-11-21 05:02

console.log("double"); vs. console.log(\'single\');

I see more and more JavaScript libraries out there using single quotes when ha

30条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-21 05:33

    I've been running the following about 20 times. And it appears that double quotes are about 20% faster.

    The fun part is, if you change part 2 and part 1 around, single quotes are about 20% faster.

    //Part1
    var r='';
    var iTime3 = new Date().valueOf();
    for(var j=0; j<1000000; j++) {
        r+='a';
    }
    var iTime4 = new Date().valueOf();
    alert('With single quote : ' + (iTime4 - iTime3));  
    
    //Part 2                
    var s="";
    var iTime1 = new Date().valueOf();
    for(var i=0; i<1000000; i++) {
        s += "a";
    }
    var iTime2 = new Date().valueOf();
    alert('With double quote: ' + (iTime2 - iTime1));
    

提交回复
热议问题