Skip to content

Commit 3822696

Browse files
authored
Merge pull request #78 from keithamus/speed-up-tostringtag-objects
Speed up tostringtag objects
2 parents 1478144 + 97e785b commit 3822696

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

bench/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ try {
3838
} catch (error) {
3939
console.error('cannot benchmark generator functions');
4040
}
41+
[
42+
'Float64Array', 'Float32Array',
43+
'Uint32Array', 'Uint16Array', 'Uint8Array',
44+
'Int32Array', 'Int16Array', 'Int8Array',
45+
'Uint8ClampedArray',
46+
].forEach(function (value) {
47+
if (typeof global[value] === 'function') {
48+
fixtures[value + new Array(19 - value.length).join(' ')] = new (global[value])(1);
49+
}
50+
});
51+
if (typeof DataView === 'function') {
52+
fixtures['DataView '] = new DataView(new ArrayBuffer(1));
53+
}
4154

4255
var filter = process.argv[2] || '';
4356
Object.keys(fixtures).filter(function (key) {

index.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,34 @@ module.exports = function typeDetect(obj) {
196196
}
197197
}
198198

199-
if (getPrototypeOfExists && (symbolToStringTagExists === false || typeof obj[Symbol.toStringTag] === 'undefined')) {
199+
/* ! Speed optimisation
200+
* Pre:
201+
* Float64Array x 625,644 ops/sec ±1.58% (80 runs sampled)
202+
* Float32Array x 1,279,852 ops/sec ±2.91% (77 runs sampled)
203+
* Uint32Array x 1,178,185 ops/sec ±1.95% (83 runs sampled)
204+
* Uint16Array x 1,008,380 ops/sec ±2.25% (80 runs sampled)
205+
* Uint8Array x 1,128,040 ops/sec ±2.11% (81 runs sampled)
206+
* Int32Array x 1,170,119 ops/sec ±2.88% (80 runs sampled)
207+
* Int16Array x 1,176,348 ops/sec ±5.79% (86 runs sampled)
208+
* Int8Array x 1,058,707 ops/sec ±4.94% (77 runs sampled)
209+
* Uint8ClampedArray x 1,110,633 ops/sec ±4.20% (80 runs sampled)
210+
* Post:
211+
* Float64Array x 7,105,671 ops/sec ±13.47% (64 runs sampled)
212+
* Float32Array x 5,887,912 ops/sec ±1.46% (82 runs sampled)
213+
* Uint32Array x 6,491,661 ops/sec ±1.76% (79 runs sampled)
214+
* Uint16Array x 6,559,795 ops/sec ±1.67% (82 runs sampled)
215+
* Uint8Array x 6,463,966 ops/sec ±1.43% (85 runs sampled)
216+
* Int32Array x 5,641,841 ops/sec ±3.49% (81 runs sampled)
217+
* Int16Array x 6,583,511 ops/sec ±1.98% (80 runs sampled)
218+
* Int8Array x 6,606,078 ops/sec ±1.74% (81 runs sampled)
219+
* Uint8ClampedArray x 6,602,224 ops/sec ±1.77% (83 runs sampled)
220+
*/
221+
var stringTag = (symbolToStringTagExists && obj[Symbol.toStringTag]);
222+
if (typeof stringTag === 'string') {
223+
return stringTag.toLowerCase();
224+
}
225+
226+
if (getPrototypeOfExists) {
200227
var objPrototype = Object.getPrototypeOf(obj);
201228
/* ! Speed optimisation
202229
* Pre:

0 commit comments

Comments
 (0)