简体   繁体   中英

Is there anything more efficient than an array of objects in javascript to store large amounts of data?

My application gathers acceleration 10 times a second, therefore I have a need to store thousands of rows of data. Currently I have an object which is a list of other objects to store it all - anything more efficient than that?

var arr = [];

function dataPoint(x, y, z, tstamp) {
    this.xAccel = x;
    this.yAccel = y;
    this.zAccel = z;
    this.epoch = tstamp;
}

var dp = new dataPoint( acceleration.x, acceleration.y, acceleration.z, acceleration.timestamp );

arr.push(dp);

If the platform you're targeting supports typed arrays then those would probably be more efficient.

Looking at your code:

> var arr = [];  
> function dataPoint(x, y, z, tstamp) {
>     this.xAccel = x;
>     this.yAccel = y;
>     this.zAccel = z;
>     this.epoch = tstamp;
> }
> var dp = new dataPoint( acceleration.x, acceleration.y,
>          acceleration.z, acceleration.timestamp );
> arr.push(dp); 

I don't see why you're bothering with a constructor, you could simply do (abbreviated):

  arr.push({x: acc.x, y: acc.y, z: acc.z, epoch: acc.timestamp});

though I'd probably change epoch to t . And given that you have such as simple structure, why not:

  arr.push([acc.x, acc.y, acc.z, acc.timestamp]);

and reference the members by index. The advantage of a constructor is inherited methods, but if you only have one level of inheritance, simple functions are just a effective and probably faster to execute (lookups on the global object are generally very fast compared to [[Prototype]] chains, but see the caveat below)

Incidentally, I think the name epoch is being used in the wrong context. An epoch is a reference point, so you might have an epoch of say 2012-04-20T18:53:21.098Z and then have deltas from that in units like milliseconds or seconds. The epoch for javascript time references (ie the value returned by Date.prototype.getTime ) is 1970-01-01T00:00:00Z, the time reference is a delta from that.

Caveat

In regard to what is "best", I presume your criterion is "fastest", with the caveat that the code must still be reasonably clear and maintainable. Your best bet there is to avoid obvious speed issues, then test in a wide variety of browsers and optimise based on results. An optimisation in one browser may have little or no difference in others, or may be significantly slower. eg decrementing loops used to be faster in most browsers, but much slower in Opera. Also, evaluating expressions in loop conditions, like:

  while ((el = node.firstChild))

were once much slower than:

  while (el) {
    el = node.firstChild

but it doesn't seem to matter as much with newer browsers. Similarly, for loops are just as fast (generally) as while and do loops. but take more typing to setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM