From c8d8d16a7eac0977dac6213ce5b1832bd6227da9 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 5 Jul 2011 11:22:23 -0400 Subject: [PATCH] Optimized selector function --- kis.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/kis.js b/kis.js index b9ceddd..462131e 100644 --- a/kis.js +++ b/kis.js @@ -31,8 +31,23 @@ */ $ = function (a) { + var x; if (typeof a !== "string"){ return a;} - var x = document.querySelectorAll(a); + + //Pick the quickest method for each kind of selector + if(a.match(/^#([\w\-]+$)/)) + { + return document.getElementById(a.split('#')[1]); + } + else if(a.match(/^([\w\-]+)$/)) + { + x = document.getElementsByTagName(a); + } + else + { + x = document.querySelectorAll(a); + } + return (x.length === 1) ? x[0] : x; }; @@ -48,7 +63,7 @@ var support = { attachEvent: typeof window.attachEvent === "function", addEventListener: typeof window.addEventListener === "function", - querySelector: typeof document.querySelectorAll === "function", + querySelector: typeof document.querySelectorAll === "function" }; $_.support = support;