2

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

user2918741
  • 87
  • 1
  • 10

2 Answers2

3

This is your answer See in Detail....

Prashant Shilimkar
  • 8,402
  • 13
  • 54
  • 89
1

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.

Steven Pribilinskiy
  • 1,862
  • 1
  • 19
  • 21