Created
August 22, 2011 17:57
-
-
Save wchiquito/1163036 to your computer and use it in GitHub Desktop.
v.1/Ejercicio 4/Reunión #4/Grupo de Estudio JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var _STRING = "string", _ARRAY = "array", _HELLO = "Hola", _SPACE = " "; | |
| function toType(obj) { | |
| //return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase(); | |
| //Expresión regular mejorada | |
| //return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase(); | |
| // Usando el "slice" | |
| return ({}).toString.call(obj).slice(8,-1).toLowerCase(); | |
| } | |
| function isArray(input) { | |
| return toType(input) === _ARRAY; | |
| } | |
| function isString(input) { | |
| return toType(input) === _STRING; | |
| } | |
| function convertString(input) { | |
| (toType(input) === _STRING && (input = input.trim())) || (input = ""); | |
| return input; | |
| } | |
| function getNamesFromArray(input) { | |
| var names = [], | |
| namesMaxLen = input.length, | |
| item, | |
| current; | |
| for (item = namesMaxLen; item > 0; --item) { | |
| current = input[namesMaxLen - item]; | |
| if (isString(current)) { | |
| names.push(current); | |
| } else if (isArray(current)) { | |
| names = names.concat(getNamesFromArray(current)); | |
| } | |
| } | |
| return names; | |
| } | |
| var crearSaludo = function(input) { | |
| var result, | |
| current, | |
| names, | |
| namesMaxLen, | |
| subNamesMaxLen, | |
| item, | |
| subItem, | |
| funcSaludo; | |
| var saludo = function() { | |
| current = convertString(input); | |
| if (current.length > 0) { | |
| result = _HELLO + _SPACE + current; | |
| } | |
| return result; | |
| }; | |
| if (isArray(input)) { | |
| funcSaludo = [], names = [], namesMaxLen = input.length; | |
| for (item = namesMaxLen; item > 0; --item) { | |
| current = input[namesMaxLen - item]; | |
| if (isString(current)) { | |
| funcSaludo.push(crearSaludo(current)); | |
| } else if (isArray(current)) { | |
| names = getNamesFromArray(current); | |
| subNamesMaxLen = names.length; | |
| for (subItem = subNamesMaxLen; subItem > 0; --subItem) { | |
| funcSaludo.push(crearSaludo(names[subNamesMaxLen - subItem])); | |
| } | |
| } | |
| } | |
| } else { | |
| funcSaludo = saludo; | |
| } | |
| return funcSaludo; | |
| }; |
Author
Según mis datos la más lenta es precisamente esa
var getObjectType2 = function(obj) {
return {}.toString.call(obj).slice(8,-1);
}Por cierto los paréntesis son innecesarios como puedes ver.
Author
Mis pruebas http://jsperf.com/test-getobjecttype
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Válidos los comentarios, muchas gracias.
Object.prototype.toString, luego de varias pruebas de rendimiento el uso de expresiones regulares penaliza bastante para el objetivo que deseamos, por lo que la opción delslicees mejor, sin embargo, una función como:presentó mejor desempeño que usando
Object.prototype.toString.[a-zA-Z]. En cuanto al uso de expresiones regulares, para este caso en particular las pruebas de rendimiento indican que es mejor evitarlas.