let is = require( '../is' );

let cache = function( fn, name ){
  return function traversalCache( arg1, arg2, arg3, arg4 ){
    let selectorOrEles = arg1;
    let eles = this;
    let key;

    if( selectorOrEles == null ){
      key = 'null';
    } else if( is.elementOrCollection( selectorOrEles ) && selectorOrEles.length === 1 ){
      key = '#' + selectorOrEles.id();
    }

    if( eles.length === 1 && key ){
      let _p = eles[0]._private;
      let tch = _p.traversalCache = _p.traversalCache || {};
      let ch = tch[ name ] = tch[ name ] || {};
      let cacheHit = ch[ key ];

      if( cacheHit ){
        return cacheHit;
      } else {
        return ( ch[ key ] = fn.call( eles, arg1, arg2, arg3, arg4 ) );
      }
    } else {
      return fn.call( eles, arg1, arg2, arg3, arg4 );
    }
  };
};

module.exports = cache;

@@ -14,17 +14,46 @@
 ire(
- 
 '../is'
- 
+);%0Alet util = require('../util'
 );%0A%0A
@@ -259,20 +259,16 @@
  key = '
-null
 ';%0A    %7D
@@ -367,14 +367,8 @@
 ey =
- '#' +
  sel
@@ -562,26 +562,67 @@
 %5B name %5D %7C%7C 
-%7B%7D
+%5B%5D;%0A      let hash = util.hashString( key )
 ;%0A      let 
@@ -636,19 +636,20 @@
 t = ch%5B 
-key
+hash
  %5D;%0A%0A   
@@ -728,19 +728,20 @@
 n ( ch%5B 
-key
+hash
  %5D = fn.

Use the hash function for traversal cache keys #2028