Skip to content

Instantly share code, notes, and snippets.

@tim-evans
Created December 1, 2011 20:59
Show Gist options
  • Save tim-evans/1419818 to your computer and use it in GitHub Desktop.
Save tim-evans/1419818 to your computer and use it in GitHub Desktop.
hashFor & guidFor optimizations
diff --git a/frameworks/runtime/core.js b/frameworks/runtime/core.js
index 2d12957..4b553f4 100644
--- a/frameworks/runtime/core.js
+++ b/frameworks/runtime/core.js
@@ -316,8 +316,6 @@ SC.mixin(/** @scope window.SC.prototype */ {
guidKey: "SproutCore" + ( SC.VERSION + Math.random() ).replace( /\D/g, "" ),
// Used for guid generation...
- _guidPrefixes: {"number": "nu", "string": "st"},
- _guidCaches: {"number": {}, "string": {}},
_numberGuids: [], _stringGuids: {}, _keyCache: {},
/**"
@@ -331,35 +329,35 @@ SC.mixin(/** @scope window.SC.prototype */ {
@returns {String} the unique guid for this instance.
*/
guidFor: function(obj) {
- var cache, ret,
- type = typeof obj;
// special cases where we don't want to add a key to object
if (obj === undefined) return "(undefined)";
if (obj === null) return "(null)";
+ var ret;
+
// Don't allow prototype changes to String etc. to change the guidFor
- if (type === SC.T_NUMBER || type === SC.T_STRING) {
- cache = this._guidCaches[type];
- ret = cache[obj];
- if(!ret) {
- ret = "st" + (jQuery.uuid++);
- cache[obj] = ret;
- }
+ switch (typeof obj) {
+ case SC.T_NUMBER:
+ ret = SC._numberGuids[obj];
+ if (!ret) ret = SC._numberGuids[obj] = "nu" + obj;
return ret;
- } else if (type === SC.T_BOOL) {
- return (obj) ? "(true)" : "(false)";
- }
- var guidKey = this.guidKey;
- if (obj[guidKey]) return obj[guidKey];
+ case SC.T_STRING:
+ ret = SC._stringGuids[obj];
+ if (!ret) ret = SC._stringGuids[obj] = "st" + (jQuery.uuid++);
+ return ret;
- // More special cases; not as common, so we check for them after the cache
- // lookup
- if (obj === Object) return '(Object)';
- if (obj === Array) return '(Array)';
+ case SC.T_BOOL:
+ return obj ? "(true)" : "(false)";
- return SC.generateGuid(obj, "sc");
+ default:
+ ret = obj[SC.guidKey];
+ if (ret) return ret;
+ if (obj === Object) return '(Object)';
+ if (obj === Array) return '(Array';
+ return SC.generateGuid(obj, 'sc');
+ }
},
/**
@@ -415,12 +413,13 @@ SC.mixin(/** @scope window.SC.prototype */ {
*/
hashFor: function() {
var l = arguments.length,
+ guidFor = SC.guidFor,
h = '',
obj, f, i;
for (i=0 ; i<l; ++i) {
obj = arguments[i];
- h += (obj && (f = obj.hash) && (typeof f === SC.T_FUNCTION)) ? f.call(obj) : this.guidFor(obj);
+ h += (obj && (f = obj.hash) && (typeof f === SC.T_FUNCTION)) ? f(obj) : guidFor(obj);
}
return h === '' ? null : h;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment