【问题标题】:Difference between JQuery.type and typeof, which is fasterJQuery.type和typeof的区别,哪个更快
【发布时间】:2013-11-24 04:03:02
【问题描述】:

我有一个名为 processData 的方法,我使用下面的代码来检查变量的类型

typeof series.dataSource.xName == "string"

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

我知道哪个更快,执行时间更短,总体“类型”需要 55ms 执行,我需要优化它

提前致谢

【问题讨论】:

  • 原始 javascript 总是比 jQuery 快。
  • 感谢您的回复,但如果我有变量 a= new Date(2000, 7,7) 包含上面的日期并且“typeof a”返回“object”,但它的日期,我该如何检查?

标签: jquery


【解决方案1】:

这是您的详细答案See....

【讨论】:

    【解决方案2】:

    使用 vanilla 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_.isWeakMap

    【讨论】:

      猜你喜欢
      • 2012-12-17
      • 2016-02-12
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多