我有一个名为processData的方法,其中我使用下面的代码来检查变量的类型
typeof series.dataSource.xName == "string"
jQuery.type(series.dataSource.xName)=="string"
我知道哪个更快,并且执行时间更短,整体“类型”需要55ms才能执行,我需要优化它
提前致谢
答案 0 :(得分:3)
这是你的答案See详细....
答案 1 :(得分:1)
使用香草JS时,它比jQuery快10到20倍。但是,在某些类型中,jQuery将提供有关该值的更多信息。所以这是区别:
╔══════════════════════════════════════════════╦════════════════════════════════════════╗
║ jQuery ║ Vanilla ║
╠══════════════════════════════════════════════╬════════════════════════════════════════╣
║ jQuery.type(null) === 'null' ║ typeof null === 'object' ║
║ ║ ║
║ jQuery.type(new Boolean()) === 'boolean' ║ typeof new Boolean() === 'object' ║
║ jQuery.type(Boolean()) === 'boolean' ║ typeof Boolean() === 'object' ║
║ jQuery.type(Object(Boolean())) === 'boolean' ║ typeof Object(Boolean()) === 'object' ║
║ ║
║ Same applies to all other Constructors i.e. same result with/without new/Object() ║
║ ║
║ jQuery.type(new Number(42)) === 'number' ║ typeof new Number(42) === 'object' ║
║ jQuery.type(new String('test')) === 'string' ║ typeof new String('test') === 'object' ║
║ jQuery.type(new Date()) === 'date' ║ typeof new Date() === 'object' ║
║ jQuery.type(new Array()) === 'array' ║ typeof new Array() === 'object' ║
║ jQuery.type(new RegExp()) === 'regexp' ║ typeof new RegExp() === 'object' ║
║ jQuery.type(new Error()) === 'error' ║ typeof new Error() === 'object' ║
║ ║ ║
║ jQuery.type([]) === 'array' ║ typeof [] === 'object' ║
║ jQuery.type(/test/) === 'regexp' ║ typeof /test/ === 'object' ║
║ ║ ║
║ jQuery.type(Symbol()) === 'symbol' ║ typeof Symbol() === 'symbol' (same) ║
║ jQuery.type(Object(Symbol())) === 'symbol' ║ typeof Object(Symbol()) === 'object' ║
╚══════════════════════════════════════════════╩════════════════════════════════════════╝
但是我建议在Lodash中使用专用方法。例如。 _.isUndefined
,_.isString
,_.isNull
,_.isDate
,_.isError
,_.isRegExp
,_.isSymbol
等。
功能更强大:
_.isNil
,_.isNaN
,_.isEmpty
,_.isArrayLike
,_.isObjectLike
,_.isPlainObject
,_.isTypedArray
,_.isSet
,{{ 1}}等。