Skip to content

Commit a00562c

Browse files
author
Ray Schamp
committed
feat(encoding): Memoize base64 encoding of assets
New assets are returned from BuiltinHelper.get, but we should only have to serialize the data once per asset.
1 parent 69fd146 commit a00562c

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/Asset.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
const TextDecoder = require('text-encoding').TextDecoder;
22

3+
const memoizedToString = (function () {
4+
const strings = {};
5+
return (assetId, data) => {
6+
if (!strings.hasOwnProperty(assetId)) {
7+
strings[assetId] = data.toString('base64');
8+
}
9+
return strings[assetId];
10+
};
11+
}());
12+
313
class Asset {
414
/**
515
* Construct an Asset.
@@ -47,7 +57,7 @@ class Asset {
4757
*/
4858
encodeDataURI (contentType) {
4959
contentType = contentType || this.assetType.contentType;
50-
return `data:${contentType};base64,${this.data.toString('base64')}`;
60+
return `data:${contentType};base64,${memoizedToString(this.assetId, this.data)}`;
5161
}
5262
}
5363

0 commit comments

Comments
 (0)