加速 Node.js 数组操作

发布于 2024-12-10 01:10:13 字数 540 浏览 0 评论 0原文

我需要将一个巨大的文件转换为以下格式:对于每个字节,将该字节打印为字符并在括号中打印其十进制值,以便“@”变为“@[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉态萌生 2024-12-17 01:10:13

如果文件是 utf-8,您可能需要先对其进行解码。另外,不要使用二进制编码,它现在基本上已被弃用。

举个例子:

var fs = require('fs')
  , StringDecoder = require('string_decoder').StringDecoder;

var stream = fs.createReadStream('/path/to/my/file')
  , decode = new StringDecoder('utf8')
  , out = [];

stream.on('data', function(buff) {
  buff = decode.write(buff);
  for (var i = 0, l = buff.length; i < l; i++) {
    out.push(buff[i] + ' [' + buff.charCodeAt(i) + ']');
  }
});

stream.on('end', function() {
  console.log(out.join('\n'));
});

为了更简单,你也可以使用读流内置的解码功能: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:

var fs = require('fs')
  , StringDecoder = require('string_decoder').StringDecoder;

var stream = fs.createReadStream('/path/to/my/file')
  , decode = new StringDecoder('utf8')
  , out = [];

stream.on('data', function(buff) {
  buff = decode.write(buff);
  for (var i = 0, l = buff.length; i < l; i++) {
    out.push(buff[i] + ' [' + buff.charCodeAt(i) + ']');
  }
});

stream.on('end', function() {
  console.log(out.join('\n'));
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文