-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSequence.benchmark.js
42 lines (29 loc) · 1.13 KB
/
Sequence.benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const timeInLoop = require('../x/timeInLoop')
const objectProto = Object.prototype
const nativeObjectToString = objectProto.toString
const objectToString = value => nativeObjectToString.call(value)
const symbolIterator = Symbol.iterator
const symbolAsyncIterator = Symbol.asyncIterator
const generatorFunctionTag = '[object GeneratorFunction]'
const asyncGeneratorFunctionTag = '[object AsyncGeneratorFunction]'
const isGeneratorFunction = value => {
const tag = objectToString(value)
return tag == generatorFunctionTag || tag == asyncGeneratorFunctionTag
}
/*
* @name Sequence.isSequence
*
* @benchmark
* isSequence1([]): 1e+8: 103.083ms
* isSequence2([]): 1e+8: 102.643ms
*/
const isSequence1 = value => value != null
&& (Boolean(value[symbolIterator])
|| Boolean(value[symbolAsyncIterator])
|| isGeneratorFunction(value))
const isSequence2 = value => value != null
&& (typeof value[symbolIterator] == 'function'
|| typeof value[symbolAsyncIterator] == 'function'
|| isGeneratorFunction(value))
// timeInLoop('isSequence1([])', 1e8, () => isSequence1([]))
// timeInLoop('isSequence2([])', 1e8, () => isSequence2([]))