Javascript 使用循环壁时间优化函数性能

Javascript 使用循环壁时间优化函数性能,javascript,optimization,Javascript,Optimization,我使用下面的函数更新并调用drawAccel()生成动画条形图的函数 function messagecb(header, message) { if(header.type == 6) { // processEchoReply(message); } else if(header.type == 4) { // accel var accels = message.b64UnpackAccelMsg(); for

我使用下面的函数更新并调用
drawAccel()生成动画条形图的函数

function messagecb(header, message) {
    if(header.type == 6) {
       // processEchoReply(message);

   } else if(header.type == 4) {
        // accel
        var accels = message.b64UnpackAccelMsg();

        for(var index = 0; index < accels.length; ++index) {
            var accel = accels[index];
            var totalClock = accelEpochAdjust(accel.clock);

            addAccelDatum(totalClock, accel.x,  accel.y, accel.z);

        }

    if ( typeof messagecb.counter == 'undefined' ) {
      messagecb.counter = 0;
    }

    ++messagecb.counter;
    if (messagecb.counter % 10 === 0) {
      drawAccel();
    }

    } else if(header.type == 3) {
       // info
       var info2 = message.b64UnpackInfo2Msg();

       displayCurrentPosition(info2.fixtime, info2.lat, info2.lon, info2.alt);
       displayMobileStatus(info2.rssi, info2.bandClass, info2.batt);

    } else if(header.type == 11) {
        btReceive(header, message);
}

我的问题是我不确定如何获取循环的最后输入时间和最后退出时间,以便运行此条件。有什么想法吗?谢谢

我不清楚你到底想做什么,但像这样的事情应该能让你接近
+new Date()
将给出自1970年1月1日以来的毫秒数,因此在不同的地方进行相同的调用应该能够得到您想要的

var start = +new Date();

for(var index = 0; index < accels.length; ++index) {
    var accel = accels[index];
    var totalClock = accelEpochAdjust(accel.clock);

    var current = +new Date();
    var timeElapsedInMs = current - start;
    //not sure the exact test you want to run here

    addAccelDatum(totalClock, accel.x,  accel.y, accel.z);
}
var lastEntered, lastExisted = +new Date();

for(var index = 0; index < accels.length; ++index) {
    lastEntered = +new Date();

    var accel = accels[index];
    var totalClock = accelEpochAdjust(accel.clock);

    var timeElapsedInMs = current - start;
    //not sure the exact test you want to run here

    addAccelDatum(totalClock, accel.x,  accel.y, accel.z);

    lastExisted = +new Date();
}
var start=+新日期();
对于(var指数=0;指数

根据您的评论进行编辑。所以,如果您总是希望有一个lastEntered和LastExit值,那么类似这样的内容可能就是您想要的

var start = +new Date();

for(var index = 0; index < accels.length; ++index) {
    var accel = accels[index];
    var totalClock = accelEpochAdjust(accel.clock);

    var current = +new Date();
    var timeElapsedInMs = current - start;
    //not sure the exact test you want to run here

    addAccelDatum(totalClock, accel.x,  accel.y, accel.z);
}
var lastEntered, lastExisted = +new Date();

for(var index = 0; index < accels.length; ++index) {
    lastEntered = +new Date();

    var accel = accels[index];
    var totalClock = accelEpochAdjust(accel.clock);

    var timeElapsedInMs = current - start;
    //not sure the exact test you want to run here

    addAccelDatum(totalClock, accel.x,  accel.y, accel.z);

    lastExisted = +new Date();
}
var lastEntered,lastExisted=+新日期();
对于(var指数=0;指数

从那里你可以做任何你需要的比较。

我不清楚你到底想做什么,但像这样的事情应该会让你接近
+new Date()
将给出自1970年1月1日以来的毫秒数,因此在不同的地方进行相同的调用应该能够得到您想要的

var start = +new Date();

for(var index = 0; index < accels.length; ++index) {
    var accel = accels[index];
    var totalClock = accelEpochAdjust(accel.clock);

    var current = +new Date();
    var timeElapsedInMs = current - start;
    //not sure the exact test you want to run here

    addAccelDatum(totalClock, accel.x,  accel.y, accel.z);
}
var lastEntered, lastExisted = +new Date();

for(var index = 0; index < accels.length; ++index) {
    lastEntered = +new Date();

    var accel = accels[index];
    var totalClock = accelEpochAdjust(accel.clock);

    var timeElapsedInMs = current - start;
    //not sure the exact test you want to run here

    addAccelDatum(totalClock, accel.x,  accel.y, accel.z);

    lastExisted = +new Date();
}
var start=+新日期();
对于(var指数=0;指数

根据您的评论进行编辑。所以,如果您总是希望有一个lastEntered和LastExit值,那么类似这样的内容可能就是您想要的

var start = +new Date();

for(var index = 0; index < accels.length; ++index) {
    var accel = accels[index];
    var totalClock = accelEpochAdjust(accel.clock);

    var current = +new Date();
    var timeElapsedInMs = current - start;
    //not sure the exact test you want to run here

    addAccelDatum(totalClock, accel.x,  accel.y, accel.z);
}
var lastEntered, lastExisted = +new Date();

for(var index = 0; index < accels.length; ++index) {
    lastEntered = +new Date();

    var accel = accels[index];
    var totalClock = accelEpochAdjust(accel.clock);

    var timeElapsedInMs = current - start;
    //not sure the exact test you want to run here

    addAccelDatum(totalClock, accel.x,  accel.y, accel.z);

    lastExisted = +new Date();
}
var lastEntered,lastExisted=+新日期();
对于(var指数=0;指数

从这里,您可以进行任何需要的比较。

这看起来越来越接近了,因为它提供了当前时间和开始时间。”var timeElapsedInMs’在我看来是‘lastExitedTime’,而current显然是当前时间,所以现在我只需要知道如何获取循环最后输入的时间。@Stavros_S-close的确-我的上次编辑应该会让您更接近。谢谢!我将在今晚对它进行破解,并让你知道它是如何工作的。由于它提供了当前时间和开始时间,看起来这已经很接近了var timeElapsedInMs’在我看来是‘lastExitedTime’,而current显然是当前时间,所以现在我只需要知道如何获取循环最后输入的时间。@Stavros_S-close的确-我的上次编辑应该会让您更接近。谢谢!今晚我会破解它,让你知道它是如何运作的。