字符串转换为undefined / null / number / boolean

时间:2013-09-14 08:07:45

标签: javascript node.js

你知道将字符串转换为它所代表的类型的更好更快的方法吗?

我一直在使用此功能:

var convertType = function (value){
    if (value === "undefined") return undefined;
    if (value === "null") return null;
    if (value === "true") return true;
    if (value === "false") return false;
    var v = Number (value);
    return isNaN (v) ? value : v;
};

考生:

//Me, Gabriel Llamas
var cast1 = function (value){
    if (value === "undefined") return undefined;
    if (value === "null") return null;
    if (value === "true") return true;
    if (value === "false") return false;
    var v = Number (value);
    return isNaN (v) ? value : v;
};

//KooiInc
var cast2 = function (value){
    var v = Number (value);
    return !isNaN(v) ? v : 
             value === "undefined" ? undefined
         : value === "null" ? null
         : value === "true" ? true
         : value === "false" ? false
         : value
};

//LightStyle
var cast3 = function (value){
    try {
        return (new Function("return " + value + ";"))();
    } catch(e) {
        return value;
    }
};

//Emmissary's proposal
var cast4 = function (value){
    if (value === "undefined") return undefined;
    try{
        return JSON.parse (value);
    }catch (e){
        return value;
    }
};

基准代码(使用speedy):

var fn = function (cast){
    cast ("undefined");
    cast ("null");
    cast ("true");
    cast ("false");
    cast ("12");
    cast ("12.34");
    cast ("asd");
};

speedy.run ({
    "cast 1": function (){
        fn (cast1);
    },
    "cast 2": function (){
        fn (cast2);
    },
    "cast 3": function (){
        fn (cast3);
    },
    "cast 4": function (){
        fn (cast4);
    }
});

结果:

File: string-conversion.js

Node v0.10.18
V8 v3.14.5.9
Speedy v0.0.8

Benchmarks: 4
Timeout: 1000ms (1s 0ms)
Samples: 3
Total time per benchmark: ~3000ms (3s 0ms)
Total time: ~12000ms (12s 0ms)

Higher is better (ops/sec)

cast 1
  6,270,458 ± 0.2%
cast 2
  3,305,103 ± 0.0%
cast 3
  54,952 ± 0.0%
cast 4
  82,790 ± 0.4%

Elapsed time: 12109ms (12s 109ms)

3 个答案:

答案 0 :(得分:4)

这是一个简单的函数,它涉及使用函数来计算字符串。通过这种方式,您可以删除部分案例的“切换”。请注意,这也处理全局变量的赋值,所以我建议只有当你知道源的来源时(不允许用户使用这个函数!)

var convertType = function (value){
    try {
        return (new Function("return " + value + ";"))();
    } catch(e) {
        return value;
    }
};

您可以看到jsfiddle here

答案 1 :(得分:3)

怎么样:

var convertType = function (value){
  var values = {undefined: undefined, null: null, true: true, false: false}
     ,isNumber = !isNaN(+(value));
  return isNumber && +(value) || !(value in values) && value || values[value];
};
convertType('null');      //=> null
convertType('something'); //=> "something"
convertType('57.321');    //=> 57.321
convertType('undefined'); //=> undefined

这似乎更快@ jsPerf

var convertType = function (value){
    var v = Number (value);
    return !isNaN(v) ? v : 
         value === "undefined" ? undefined
       : value === "null" ? null
       : value === "true" ? true
       : value === "false" ? false
       : value
 }

答案 2 :(得分:2)

var string2literal = function (value){
  var maps = {
   "NaN": NaN,
   "null": null,
   "undefined": undefined,
   "Infinity": Infinity,
   "-Infinity": -Infinity
   }
  return ((value in maps) ? maps[value] : value);
};

动态数据类型转换有许多奇怪的规则,只需映射它。