Skip to content

Opening full file is slow and useless #27

@maxired

Description

@maxired

Hi,

when we give a filename to the library, it use fs.readfile to open the file.
Looks like the whole file is open, which is slow for big pictures.

I suggest that we first open the header of the picture and then read only the bytes corresponding to the exif part.

I would love to submit a pull request for this, but since there is not unit test (issue #26), I am not sure not to break anything.

I am thinking about something like :

var firstReadLength=6;
fs.open(image, 'r', function(err, fd) {
  var data = new Buffer(firstReadLength);
  fs.read(fd, data, 0, firstReadLength, null, function(err, length, buffer) {
    if (buffer[0] == 0xFF && buffer[1] == 0xd8) {
      if (buffer[2] == 0xff && buffer[3] == 0xe1) {
        var length = buffer.readUInt16BE(4);
        var exifBuffer = new Buffer(length);
        fs.read(fd, exifBuffer, 0, length , 0, function(err, length, buffer) {
          processImage(buffer, callback);
        });
      }
    }
}

In my case, this lead to significant performance improvement : >*6 with pictures of around 7MB each (dont forget to drop your cache when doing this kind of performance analysis )

 echo 3 | sudo tee /proc/sys/vm/drop_caches

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions