js创建对象的四种方式以及消耗时间比较
// Property-less object console.time(‘zoop1‘); var zoop = (function (numObjects) { var pooz = Object.create(null); for(var i = 0; i< numObjects; i++) { pooz[i] = Object.create(null); } return pooz; })(1000000); console.timeEnd(‘zoop1‘); // Using Object Literal console.time(‘zoop2‘); var zoop = (function (numObjects) { var pooz = {}; for(var i = 0; i< numObjects; i++) { pooz[i] = {}; } return pooz; })(1000000); console.timeEnd(‘zoop2‘); // Object.create({}) console.time(‘zoop3‘); var zoop = (function (numObjects) { var pooz = Object.create({}); for(var i = 0; i< numObjects; i++) { pooz[i] = Object.create({}); } return pooz; })(1000000); console.timeEnd(‘zoop3‘); // new Object console.time(‘zoop4‘); var zoop = (function (numObjects) { var pooz = new Object(); for(var i = 0; i< numObjects; i++) { pooz[i] = new Object(); } return pooz; })(1000000); console.timeEnd(‘zoop4‘);
来源:https://gist.github.com/shubhadeep/371f9245656d5937af09
消耗的时间从少到多>>: 2时间总是最少的,其他三个有变动,一般3消耗时间最多。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。