CocosCreater全局变量耗时测试

本文通过代码测试比较了CocosCreator中使用全局变量与模块化方式的运行速度,同时测试了localstorage的性能。结果显示了不同方式下的运行效率差异。

之前用到了全局变量,很好奇模块化跟常驻节点哪个运行快一点,写个代码测试下运行时间。
先放结果顺便测试了下localstorage 在这里插入图片描述
代码如下

  1. 创建个模块化的全局变量
module.exports = {
    atest: 0,
};
  1. 创建个常驻节点,加上代码
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;
    },
});
  1. 开始测试
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) {

    // },
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值