只为粗暴看一下ES6的字符串模板的性能

网上查找"ES6 字符串模板 +性能"5分钟无果遂写了一个暴力测试。

测试对象

  • +=方式,字符串累加计算方式
  • +s1+s2...+sn方式,即传统连加拼接字符串方式
  • s.push(s1); s.join('')方式,数组拼接字符串方式
  • es6字符串模板

测试方式:各执行times次运算,记录执行时间。

测试结果

字符串模板的确钢!

不过上述是在Chrome中的测试结果,结果显示字符串模板的性能和字符串连加拼接方式性能大体相当,是+=逐个链接方式性能的5倍左右,array join方式性能最差,不忍直视。

在火狐浏览器下,除了array join依然不忍直视外,其他三种性能表现相当:

,火狐下贴一张撑撑门面就行了。

微软Edge下,结果比较呵呵哒:

除了连加方式外,其他基本上都可以算挂了。。。

IE呢?压根就不支持字符串模板,所以咱就不测了。

下面是测试脚本

  1 (function () {
  2 
  3     let stringPlus = function () {
  4 
  5             //+=运算
  6 
  7             let a = "ahsdgdsd'sadbd'fpdfknsndf";
  8             a += "sgeoejhsshdsksdlkfkfss";
  9             a += "weieotjmn;gjfkawjrbnvjkjke";
 10             a += "sdfnjslkdgfpla;'gjfak";
 11             a += "dsfjpojjagjksfdoaaskksgsf;adn";
 12             a += "wyehfdhfvcxaawjyfbsvkcsajgef";
 13             a += "igvbcxzxgfhpgbsna";
 14             a += "afsvcbhlksjegjfdnalrllfdjnxl";
 15             a += "awshbnlsklesfdnxsxldxdsdhkjklm";
 16             a += "uatesgsjdabsuaadjhcbdsdgcdbchnxkyesuefgdgsdxjhdbxcduhdywgbsihs";
 17             a += "agsccbjzhbcxbwefyouoiUWA8YESSIHCB";
 18             a += "dfasdkcaehwieruefandjcnxzvcuerefha";
 19             a += "vsdrleifhjs489wodjnlgsehufdja";
 20             a += "wegtfyudashnvnxntrwefkhjhfnc";
 21             a += "ASDGFODIUERTGYHJG;OLFJAMGRTYRUGFNDLAKBWYG ";
 22             a += "WEGPFHAFSDRRGJNRYTE8Aghjfoueygf";
 23             a += "erfvcdvmcdlfklvcnssejckzdjvcns";
 24             a += "wetufvnckjffbcnmfwuirldncxmlzjn";
 25             a += "fhfhjlkgeiuvlzkjdquevxx,.vnee";
 26             a += "sdchbnzkdn xxcmsbvcosihhsjm.mn soe";
 27             a += "yuuweygfyvczhvqwerhbfvnsjlhvzxv";
 28             a += "qyylkldbhjjfjJHMNVlaefl";
 29             a += "bdkvxknkniruvn,xhlsaeshksjfk z,";
 30             a += "ygufdkfhakvbxcv,nu[gq;hfauhfaks";
 31             a += "asfyerueiwsnjlkZBcharvbhzcablf";
 32             a += "dvcbzkjnvlsujvcz,cwrajklzldnahruf";
 33             a += "siihjscdcnlzzcslfahdwuiednvcaesf";
 34             a += "auhdzcjsfhjsl;ncaaureiwksnvlcl";
 35             a += "iuaehcnslklcseufjsjeotroeiosdn";
 36             a += "waeg;tirueywtrdfghjnxlghfgekfg";
 37             a += "nhgfdhjkf,v'c;lIkzxjfhbnszdv";
 38             a += "zxgfdsuyhjkglckjlYATRGFHJKXJCHASFJ";
 39             a += "afyvhjletd;LJKN;alnksrjfhbhdfn";
 40             a += "bvcxhgehglalksudjhfjdncmxwpljfd";
 41             return a;
 42         },
 43         stringPlus2 = function () {
 44 
 45             //连+运算
 46 
 47             let a = "ahsdgdsd'sadbd'fpdfknsndf"
 48             + "sgeoejhsshdsksdlkfkfss"
 49             + "weieotjmn;gjfkawjrbnvjkjke"
 50             + "sdfnjslkdgfpla;'gjfak"
 51             + "dsfjpojjagjksfdoaaskksgsf;adn"
 52             + "wyehfdhfvcxaawjyfbsvkcsajgef"
 53             + "igvbcxzxgfhpgbsna"
 54             + "afsvcbhlksjegjfdnalrllfdjnxl"
 55             + "awshbnlsklesfdnxsxldxdsdhkjklm"
 56             + "uatesgsjdabsuaadjhcbdsdgcdbchnxkyesuefgdgsdxjhdbxcduhdywgbsihs"
 57             + "agsccbjzhbcxbwefyouoiUWA8YESSIHCB"
 58             + "dfasdkcaehwieruefandjcnxzvcuerefha"
 59             + "vsdrleifhjs489wodjnlgsehufdja"
 60             + "wegtfyudashnvnxntrwefkhjhfnc"
 61             + "ASDGFODIUERTGYHJG;OLFJAMGRTYRUGFNDLAKBWYG "
 62             + "WEGPFHAFSDRRGJNRYTE8Aghjfoueygf"
 63             + "erfvcdvmcdlfklvcnssejckzdjvcns"
 64             + "wetufvnckjffbcnmfwuirldncxmlzjn"
 65             + "fhfhjlkgeiuvlzkjdquevxx,.vnee"
 66             + "sdchbnzkdn xxcmsbvcosihhsjm.mn soe"
 67             + "yuuweygfyvczhvqwerhbfvnsjlhvzxv"
 68             + "qyylkldbhjjfjJHMNVlaefl"
 69             + "bdkvxknkniruvn,xhlsaeshksjfk z,"
 70             + "ygufdkfhakvbxcv,nu[gq;hfauhfaks"
 71             + "asfyerueiwsnjlkZBcharvbhzcablf"
 72             + "dvcbzkjnvlsujvcz,cwrajklzldnahruf"
 73             + "siihjscdcnlzzcslfahdwuiednvcaesf"
 74             + "auhdzcjsfhjsl;ncaaureiwksnvlcl"
 75             + "iuaehcnslklcseufjsjeotroeiosdn"
 76             + "waeg;tirueywtrdfghjnxlghfgekfg"
 77             + "nhgfdhjkf,v'c;lIkzxjfhbnszdv"
 78             + "zxgfdsuyhjkglckjlYATRGFHJKXJCHASFJ"
 79             + "afyvhjletd;LJKN;alnksrjfhbhdfn"
 80             + "bvcxhgehglalksudjhfjdncmxwpljfd";
 81             return a;
 82         },
 83         arrayJoin = function () {
 84 
 85             //array join
 86 
 87             let a = ["ahsdgdsd'sadbd'fpdfknsndf"];
 88             a.push("sgeoejhsshdsksdlkfkfss");
 89             a.push("weieotjmn;gjfkawjrbnvjkjke");
 90             a.push("sdfnjslkdgfpla;'gjfak");
 91             a.push("dsfjpojjagjksfdoaaskksgsf;adn");
 92             a.push("wyehfdhfvcxaawjyfbsvkcsajgef");
 93             a.push("igvbcxzxgfhpgbsna");
 94             a.push("afsvcbhlksjegjfdnalrllfdjnxl");
 95             a.push("awshbnlsklesfdnxsxldxdsdhkjklm");
 96             a.push("uatesgsjdabsuaadjhcbdsdgcdbchnxkyesuefgdgsdxjhdbxcduhdywgbsihs");
 97             a.push("agsccbjzhbcxbwefyouoiUWA8YESSIHCB");
 98             a.push("dfasdkcaehwieruefandjcnxzvcuerefha");
 99             a.push("vsdrleifhjs489wodjnlgsehufdja");
100             a.push("wegtfyudashnvnxntrwefkhjhfnc");
101             a.push("ASDGFODIUERTGYHJG;OLFJAMGRTYRUGFNDLAKBWYG ");
102             a.push("WEGPFHAFSDRRGJNRYTE8Aghjfoueygf");
103             a.push("erfvcdvmcdlfklvcnssejckzdjvcns");
104             a.push("wetufvnckjffbcnmfwuirldncxmlzjn");
105             a.push("fhfhjlkgeiuvlzkjdquevxx,.vnee");
106             a.push("sdchbnzkdn xxcmsbvcosihhsjm.mn soe");
107             a.push("yuuweygfyvczhvqwerhbfvnsjlhvzxv");
108             a.push("qyylkldbhjjfjJHMNVlaefl");
109             a.push("bdkvxknkniruvn,xhlsaeshksjfk z,");
110             a.push("ygufdkfhakvbxcv,nu[gq;hfauhfaks");
111             a.push("asfyerueiwsnjlkZBcharvbhzcablf");
112             a.push("dvcbzkjnvlsujvcz,cwrajklzldnahruf");
113             a.push("siihjscdcnlzzcslfahdwuiednvcaesf");
114             a.push("auhdzcjsfhjsl;ncaaureiwksnvlcl");
115             a.push("iuaehcnslklcseufjsjeotroeiosdn");
116             a.push("waeg;tirueywtrdfghjnxlghfgekfg");
117             a.push("nhgfdhjkf,v'c;lIkzxjfhbnszdv");
118             a.push("zxgfdsuyhjkglckjlYATRGFHJKXJCHASFJ");
119             a.push("afyvhjletd;LJKN;alnksrjfhbhdfn");
120             a.push("bvcxhgehglalksudjhfjdncmxwpljfd");
121             return a.join('');
122         },
123         stringTemplate = function () {
124 
125             //字符串模板
126 
127             let a1 = "ahsdgdsd'sadbd'fpdfknsndf",
128                 a2 = "sgeoejhsshdsksdlkfkfss",
129                 a3 = "weieotjmn;gjfkawjrbnvjkjke",
130                 a4 = "sdfnjslkdgfpla;'gjfak",
131                 a5 = "dsfjpojjagjksfdoaaskksgsf;adn",
132                 a6 = "wyehfdhfvcxaawjyfbsvkcsajgef",
133                 a7 = "igvbcxzxgfhpgbsna",
134                 a8 = "afsvcbhlksjegjfdnalrllfdjnxl",
135                 a9 = "awshbnlsklesfdnxsxldxdsdhkjklm",
136                 a10 = "agsccbjzhbcxbwefyouoiUWA8YESSIHCB";
137             let a = `${a1}${a2}${a3}${a4}${a5}${a6}${a7}${a8}${a9}uatesgsjdabsuaadjhcbdsdgcdbchnxkyesuefgdgsdxjhdbxcduhdywgbsihs${a10}${"dfasdkcaehwieruefandjcnxzvcuerefha"}vsdrleifhjs489wodjnlgsehufdja${"wegtfyudashnvnxntrwefkhjhfnc"}${"ASDGFODIUERTGYHJG;OLFJAMGRTYRUGFNDLAKBWYG "}${"WEGPFHAFSDRRGJNRYTE8Aghjfoueygf"}${"erfvcdvmcdlfklvcnssejckzdjvcns"}${"wetufvnckjffbcnmfwuirldncxmlzjn"}${"fhfhjlkgeiuvlzkjdquevxx,.vnee"}${"sdchbnzkdn xxcmsbvcosihhsjm.mn soe"}${"yuuweygfyvczhvqwerhbfvnsjlhvzxv"}qyylkldbhjjfjJHMNVlaefl${"bdkvxknkniruvn,xhlsaeshksjfk z,"}${"ygufdkfhakvbxcv,nu[gq;hfauhfaks"}${"asfyerueiwsnjlkZBcharvbhzcablf"}${"dvcbzkjnvlsujvcz,cwrajklzldnahruf"}${"siihjscdcnlzzcslfahdwuiednvcaesf"}${"auhdzcjsfhjsl;ncaaureiwksnvlcl"}${"iuaehcnslklcseufjsjeotroeiosdn"}${"waeg;tirueywtrdfghjnxlghfgekfg"}${"nhgfdhjkf,v'c;lIkzxjfhbnszdv"}${"zxgfdsuyhjkglckjlYATRGFHJKXJCHASFJ"}${"afyvhjletd;LJKN;alnksrjfhbhdfn"}bvcxhgehglalksudjhfjdncmxwpljfd`
138             return a;
139         };
140 
141     //stringPlus
142     let start = new Date().getTime(),
143         string = "",
144         times = 1000000;
145     console.info('操作次数 -> ' + times);
146     for(let i = 0; i < times; i++){
147         string = stringPlus();
148     }
149     let end = new Date().getTime();
150     console.info("string plus -> " + (end - start));
151 
152     //stringPlus2
153     start = new Date().getTime();
154     string = "";
155     for(let i = 0; i < times; i++){
156         string = stringPlus2();
157     }
158     end = new Date().getTime();
159     console.info("string plus 2 -> " + (end - start));
160 
161     //arrayJoin
162     start = new Date().getTime();
163     string = "";
164     for(let i = 0; i < times; i++){
165         string = arrayJoin();
166     }
167     end = new Date().getTime();
168     console.info("array join -> " + (end - start));
169 
170     //stringTemplate
171     start = new Date().getTime();
172     string = "";
173     for(let i = 0; i < times; i++){
174         string = stringTemplate();
175     }
176     end = new Date().getTime();
177     console.info("string template -> " + (end - start));
178 
179 })();

 

posted @ 2017-02-04 16:05  ferrinweb  阅读(771)  评论(0编辑  收藏  举报