W3C home > Mailing lists > Public > public-webapps@w3.org > July to September 2014

Proposal to add a method to Blob to compare them byte-by-byte

From: Douglas Sherk <dsherk@mozilla.com>
Date: Thu, 4 Sep 2014 12:58:49 -0700 (PDT)
To: public-webapps@w3.org
Message-ID: <1410205237.19094928.1409860729876.JavaMail.zimbra@mozilla.com>
Hi all,

Currently, there's no way to compare the contents of two Blobs without writing your own byte-by-byte comparison. A use case I can see for this is setting webapp wallpapers and wanting to see if an image that a user uploaded is the same as one already in a gallery, but without having to query a server or store them as anything other than blobs.

I have an example of what the byte-by-byte comparison code looks like. [1]

Here's a much simpler test case:

```
<script>
  var blobA = new Blob(['hello'], {type: 'text/html'});
  var blobB = new Blob(['hello'], {type: 'text/html'});

  // Prints "blobA and blobB are not equal"
  alert('blobA and blobB are ' + (blobA == blobB ? 'equal' : 'not equal'));
</script>
```

Obviously, since byte-by-byte blob comparisons can be very expensive, I think it's dangerous to provide this with a simple "==" check, so I don't actually think we should do anything about this. However, Jonas Sicking proposed an interface like this:

```
p = blob.isEqual(otherblob);
p.then((equal) => {
  if (equal === true) {
    alert("they were equal");
  }
  else {
    alert("they were not equal");
  }
});
```

This would guarantee the following about the blobs:
1) they are the same MIME type,
2) they are byte-by-byte equal.

It could also perform shortcuts (note: these are implementation details) such as:
1) checking the sizes and if they're different, bailing out early,
2) using memcmp and such when appropriate.

This is discussed further on Mozilla's bug tracker. [2]

Doug

[1] https://bug1029064.bugzilla.mozilla.org/attachment.cgi?id=8445567
[2] https://bugzilla.mozilla.org/show_bug.cgi?id=1029064
Received on Friday, 5 September 2014 07:14:03 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 18:14:26 UTC