加速 Node.js 数组操作
我需要将一个巨大的文件转换为以下格式:对于每个字节,将该字节打印为字符并在括号中打印其十进制值,以便“@”变为“@[64]”。我得到以下信息:
var s = fs.createReadStream(path, { encoding: 'binary' });
var p = [];
s.addListener('data', function(data) {
for(var i = 0; i < data.length; i++) {
p.push(data.charAt(i) + "[" + data.charCodeAt(i) + "]");
}
});
s.addListener('end', function(data) {
console.log(p.join(""));
});
然而,这种工作速度非常慢。有什么想法可以优化吗?
更新。根据评论,添加了一些日期语句,发现“p.push”行占用了大部分时间。所以我猜问题不在于文件读取。尽管如此,问题仍然存在——如何加快速度。
I need to convert a huge file into the following format: for each byte, print that byte as character and print its decimal value in brackets, so that "@" becomes "@[64]". I've got the following:
var s = fs.createReadStream(path, { encoding: 'binary' });
var p = [];
s.addListener('data', function(data) {
for(var i = 0; i < data.length; i++) {
p.push(data.charAt(i) + "[" + data.charCodeAt(i) + "]");
}
});
s.addListener('end', function(data) {
console.log(p.join(""));
});
This kind of works, however, is totally slow. Any ideas how this can be optimized?
upd. as per the comment, added some date statements and found out that the "p.push" line takes most of the time. So I guess the problem is not in file reading. Still, the question remains - how to speed this up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果文件是 utf-8,您可能需要先对其进行解码。另外,不要使用二进制编码,它现在基本上已被弃用。
举个例子:
为了更简单,你也可以使用读流内置的解码功能:
stream.setEncoding('utf-8');
而不用自己制作StringDecoder,但是它在某些情况下,使用您自己的缓冲区可能很有用,例如您仍然可以直接访问缓冲区。编辑:抱歉,没有看到您要求优化。你所做的事情本质上很慢。我以为你只是想用它来调试目的。可能有一种更好的方法来完成您最初想做的事情。但是,由于您无论如何都会缓冲所有内容,因此您不妨执行
fs.readFile
并遍历缓冲区。If the file is utf-8, you're going to probably want to decode it first. Also, don't use the binary encoding, it's basically deprecated by now.
Here's an example:
To make it simpler, you can also use the built-in decoding feature of read streams:
stream.setEncoding('utf-8');
instead of making your own StringDecoder, but it can be useful using your own in some cases, e.g. you still have direct access to the buffer.Edit: Sorry, didn't see you were asking for an optimization. What you're doing is inherently slow. I thought you only wanted it for debugging purposes. There might just be a better way of doing what you're trying to do in the first place. However, since you're buffering everything anyway, you might as well just do an
fs.readFile
and walk over the buffer.