0

I have a similar issue to this: https://github.com/Huddle/Resemble.js/issues/57.

However he says that he is using promises and async to loop over, so that it takes 4-5 to do 900 images! Currently for me it takes about 2-3 seconds per image with this code below:

files.forEach(function (file) {
  var diff = resemble(path.join(__dirname, './../../uploads/' + file))
    .compareTo(path.join(__dirname, './../../uploads/' + req.file.filename))
    .ignoreColors()
    .onComplete(function(data){
      console.log(data.misMatchPercentage);
    });
});

Can anyone explain how to use async and promises to improve the code that I have written, so that it executes faster? I'm not even entirely sure how to use promises in this context, and have only really limited experience with them.

As you can see in my code it just compares the images sequentially, I presume with promises it does not?

Thanks and I appreciate the help.

Frxstrem
  • 38,761
  • 9
  • 79
  • 119
SwimmingG
  • 654
  • 2
  • 9
  • 29
  • 1
    It seems to be a fundamental issue with resemble. I think the original article meant that it was freezing for 4-5 seconds while processing. – Lee Taylor Oct 12 '16 at 18:13
  • Mine seems to be freezing for much longer, 20-30 seconds just for 10-15 images? Is this because I'm not using promises? – SwimmingG Oct 12 '16 at 18:15
  • I don't think it's going to get quicker. I don't believe you can run this in parallel (multiple threads), as threading in javascript is still quite new. Why is the speed an issue? – Lee Taylor Oct 12 '16 at 18:16
  • Because I need to return the related images, would it be better to use a C / Java / C ++ lib and call that from my node server? Not sure how to do it but I can investigate – SwimmingG Oct 12 '16 at 18:18
  • This seems like the sort of job you do once in a while. If you're needing to do this a lot then I would question your logic. Also, yes, a server-side solution is going to be faster and more robust. – Lee Taylor Oct 12 '16 at 18:19
  • This already is being done on the server side? I'm trying to create a search by image feature, that is the logic behind it having to be called frequently. – SwimmingG Oct 12 '16 at 18:20
  • 2
    Ah, ok. I assumed (incorrectly) that you were doing this in the browser (as per jamescryer's reply in your link). Is there no way you can cache the processing that's already done for each image? It seems like it's going to be a lot of work to compare each image with a new one each time. – Lee Taylor Oct 12 '16 at 18:23
  • That's also a great suggestion. Thank you alot! – SwimmingG Oct 12 '16 at 18:48

0 Answers0