node中使用es6/7/8(支持性与性能)(2)

var i = 0; var start = +new Date(), duration; var person = { name: 'wayne', age: 21, school: 'xjtu' } while (i++ < 100000000) { var str = `Hello, I am ${person.name}, and I am ${person.age} years old, I come from ${person.school}`; } duration = +new Date() - start; console.log(duration)

经过测试,可以发现耗时分别为 2978/3022/3010。

经过计算,使用es6的语法耗时是es5语法的1.25倍左右。 因此,尽量减少在node端使用模板字符串,如果大量使用,显然耗时是非常严重的。

箭头函数

es5语法:

var i = 0; var start = +new Date(), duration; var func = {}; while (i++ < 10000000) { func[i] = function (x) { return 10 + x; } } duration = +new Date() - start; console.log(duration)

经过测试,发现耗时分别为 1675/1639/1621。

es6语法:

var i = 0; var start = +new Date(), duration; var func = {}; while (i++ < 10000000) { func[i] = (x) => 10 + x } duration = +new Date() - start; console.log(duration)

经过测试,发现耗时分别为 1596/1770/1597。

即使用箭头函数的运行速度和使用es5方式的箭头函数在运行速度上是一致的,并且使用es6的箭头函数写起来更加方便,所以推荐使用,我们可以直接使用。

总结

在node端使用es6还是不错的,对于常见的class、let、箭头函数等等在速度上和es5不相上下,但是在写起来会更加方便,还是推荐使用的。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/22257b275273f54a5caf29ef5e6cce36.html