-
-
Save james2doyle/5577276 to your computer and use it in GitHub Desktop.
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
// based on https://gist.github.com/Potfur/5576225 & https://github.com/james2doyle/saltjs | |
// more info: https://plus.google.com/109231487156400680487/posts/63eZzzrBSb6 | |
window.$ = function(s) { | |
var c = { | |
'#': 'ById', | |
'.': 'sByClassName', | |
'@': 'sByName', | |
'=': 'sByTagName'}[s[0]]; | |
return document[c?'getElement'+c:'querySelectorAll'](s.slice(1)) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice short function, but it isn't similar to jQuery selectors (prefix "=") and can't parse "complex selectors" ('#three > .class2' -> returns NULL).
$.3.js size:
Input: 218 bytes
Output: 162 bytes
Gzip: 155 bytes
custom function:
input: 775 bytes
Output: 216 bytes
Gzip: 187 bytes
It isn't deeply tested yet, but seems to perform nice.
https://jsperf.com/custom-function-vs-james2doyle-vs-jquery
Have You any new / improved selector function? Any suggestions about my function how to improve?