简体   繁体   中英

Quickest way to convert from CSV to object array

I have a large string that comes across the wire using an $.ajax request. I can format the string any way necessary, and currently am using a % as the line delimiter and , as the item delimiter. Considering performance is so essential in my application, does anyone have a quicker way to do the following? Thank You

function convertCSV(s) {
    var lines = s.split("%");
    var items, sym, arr = [];

    for (var x = 0, len = lines.length; x < len; x++) {
        items = lines[x].split(",");
        sym = {};
        sym.time = +items[0];
        sym.num1 = +items[1];
        sym.num2 = +items[2];
        sym.a1 = +items[3];
        sym.b1 = +items[4];
        sym.c1 = +items[5];
        sym.d1 = +items[6];
        sym.e1 = +items[7];
        sym.f1 = +items[8];
        sym.g1 = +items[9];
        sym.h1 = +items[10];
        sym.l1 = +items[11];
        arr[x] = sym;
    }

    return arr;
}

也许JSON会对您通过线路发送的内容进行编码,然后在收到后对其进行JSON解码。

A (minor) optimisation:

function convertCSV(s) {
  var lines = s.split("%");
  var items, arr = [];
  while ((items = lines.shift()) && (items = items.split(",")) {
    arr.push({ 
        time : +items[0], num1 : +items[1],  num2 : +items[2],
        a1   : +items[3], b1   : +items[4],  c1   : +items[5],
        d1   : +items[6], e1   : +items[7],  f1   : +items[8],
        g1   : +items[9], h1   : +items[10], l1   : +items[11]
     });
  }
  return arr;
}

Could be worth experimenting with Array.shift() and Array.pop()

If you are concerned about speed, you should probably create a simply parser that will parse the string character by character.

Here's a simple example:

DEMO

function convertCSV(s, properties) {
    var result = [],
        i = 0,
        len = s.length,
        propIndex = 0,
        row = {},
        val = '',
        c;

    for (; i < len; i++) {
        switch(c = s[i]) {
            case ',':
                row[properties[propIndex++]] = val;
                val = '';
                break;
            case '%':
                result.push(row);
                row[properties[propIndex++]] = val;
                propIndex = 0;
                row = {};
                val = '';
                break;
            default:
                val += c;
        }
    }

    return result;
}

console.log(convertCSV('a,b,c%d,e,f%h,i,j%', ['a', 'b', 'c']));

EDIT:

I ran a few performance tests and it seems that I wasn`t right afterall. Your current method is actually the second fastest, but the quickest way to do this seems to be using regular expressions. I must say that I am quite surprised that the simple parser isin't the fastest solution.

PERFORMANCE TEST

var rx = /(.*?),(.*?),(.*?)%/g,
    result = [],
    match;

while (match = rx.exec(s)) {
    result.push({
        a: match[1],
        b: match[2],
        c: match[3]
    });
}

console.log(result);

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