之前用到了全局变量,很好奇模块化跟常驻节点哪个运行快一点,写个代码测试下运行时间。
先放结果顺便测试了下localstorage 
代码如下
- 创建个模块化的全局变量
module.exports = {
atest: 0,
};
- 创建个常驻节点,加上代码
cc.Class({
extends: cc.Component,
properties: {
},
// use this for initialization
onLoad: function() {
cc.game.addPersistRootNode(this.node);
},
setdata: function(testdata) {
this.data = testdata;
},
getdata: function() {
return this.data;
},
});
- 开始测试
ar com = require('Common');
cc.Class({
extends: cc.Component,
properties: {
label: {
default: null,
type: cc.Label
},
// defaults, set visually when attaching this script to the Canvas
text: 'Hello, World!'
},
// use this for initialization
onLoad: function() {
var that = this;
this.label.string = this.text;
var node = cc.find('New Node').getComponent('node');
node.setdata(0);
window.btest = 0;
//简单计算测试
console.time();
this.aruntest();
console.log("com模块化时间测试" + com.atest);
console.timeEnd();
console.time();
for (let i = 0; i <= 100000000; i++) {
node.data++;
};
console.log("常驻节点时间测试" + node.data);
console.timeEnd();
console.time();
this.bruntest();
console.log("window时间测试" + window.btest);
console.timeEnd();
//简单数据测试
console.time();
window.datatest = {
data: 0,
}
console.log("window数据时间测试" + window.datatest.data);
console.timeEnd();
console.time();
window.localStorage.setItem("data", "0")
console.log("localStorage数据时间测试" + window.localStorage.data);
console.timeEnd();
},
// called every frame
aruntest: function() {
for (let i = 0; i <= 100000000; i++) {
com.atest++;
};
},
bruntest: function() {
for (let i = 0; i <= 100000000; i++) {
window.btest++;
};
},
// update: function(dt) {
// },
});
本文通过代码测试比较了CocosCreator中使用全局变量与模块化方式的运行速度,同时测试了localstorage的性能。结果显示了不同方式下的运行效率差异。
692

被折叠的 条评论
为什么被折叠?



