Skip to content

Commit 6c14d43

Browse files
committed
Improve performance of Howl::load for long URLs
The previous regexp was highly inefficient. It took 21 SECONDS to sniff the type from a data-URI consisting of 128K characters. The culprit is /.+\.([^?]+)(\?|$/. With this patch, sniffing the type takes less than 1 millisecond for any string.
1 parent 247cfff commit 6c14d43

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

howler.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,14 @@
296296
ext = self._format;
297297
} else {
298298
// figure out the filetype (whether an extension or base64 data)
299-
urlItem = self._urls[i].toLowerCase().split('?')[0];
300-
ext = urlItem.match(/.+\.([^?]+)(\?|$)/);
301-
ext = (ext && ext.length >= 2) ? ext : urlItem.match(/data\:audio\/([^?]+);/);
299+
urlItem = self._urls[i];
300+
ext = /^data:audio\/([^;,]+);/i.exec(urlItem);
301+
if (!ext) {
302+
ext = /\.([^.]+)$/.exec(urlItem.split('?', 1)[0]);
303+
}
302304

303305
if (ext) {
304-
ext = ext[1];
306+
ext = ext[1].toLowerCase();
305307
} else {
306308
self.on('loaderror');
307309
return;

0 commit comments

Comments
 (0)