Difference between JQuery.type and typeof, which is faster

不问归期 提交于 2021-01-28 03:34:21

问题


I have a method called processData, in which i have used the below code to check the type of the variable

typeof series.dataSource.xName == "string"

jQuery.type(series.dataSource.xName)=="string"

i what to know which is faster, and will take less time to execute, overall "type" need 55ms to execute, i need to optimize it

Thanks in advance


回答1:


This is your answer See in Detail....




回答2:


It's 10x-20x times faster when using vanilla JS than jQuery. However there are a few types where jQuery will give more information about the value. So here are the differences:

╔══════════════════════════════════════════════╦════════════════════════════════════════╗
║                   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'   ║
╚══════════════════════════════════════════════╩════════════════════════════════════════╝

However I suggest to go with dedicated methods in Lodash. E.g. _.isUndefined, _.isString, _.isNull, _.isDate, _.isError, _.isRegExp, _.isSymbol, etc.

And even more powerful: _.isNil, _.isNaN, _.isEmpty, _.isArrayLike, _.isObjectLike, _.isPlainObject, _.isTypedArray, _.isSet, _.isWeakMap, etc.



来源:https://stackoverflow.com/questions/19921838/difference-between-jquery-type-and-typeof-which-is-faster

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!