[英]Quickest way to convert from CSV to object array
I have a large string that comes across the wire using an $.ajax request. 我有一个使用$ .ajax请求遇到的大字符串。 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: A(次要)优化:
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()
值得尝试使用
Array.shift()
和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: 这是一个简单的例子:
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.
我必须说,我很惊讶简单的解析器不是最快的解决方案。
var rx = /(.*?),(.*?),(.*?)%/g,
result = [],
match;
while (match = rx.exec(s)) {
result.push({
a: match[1],
b: match[2],
c: match[3]
});
}
console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.