Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8997

Show
Ignore:
Timestamp:
03/09/08 06:56:03 (7 months ago)
Author:
tobie
Message:

prototype: Performance improvements for Enumerables. Closes #11264.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • spinoffs/prototype/trunk/CHANGELOG

    r8948 r8997  
     1* Performance improvements for Enumerables. Closes #11264. [Ben, Samuel Lebeau] 
     2 
    13* deprecation extension: mark Hash.toJSON() as removed. [Tobie Langel] 
    24 
  • spinoffs/prototype/trunk/src/enumerable.js

    r8798 r8997  
    44  each: function(iterator, context) { 
    55    var index = 0; 
    6     iterator = iterator.bind(context); 
    76    try { 
    87      this._each(function(value) { 
    9         iterator(value, index++); 
     8        iterator.call(context, value, index++); 
    109      }); 
    1110    } catch (e) { 
     
    1615   
    1716  eachSlice: function(number, iterator, context) { 
    18     iterator = iterator ? iterator.bind(context) : Prototype.K; 
    1917    var index = -number, slices = [], array = this.toArray(); 
    2018    if (number < 1) return array; 
     
    2523 
    2624  all: function(iterator, context) { 
    27     iterator = iterator ? iterator.bind(context) : Prototype.K; 
     25    iterator = iterator || Prototype.K; 
    2826    var result = true; 
    2927    this.each(function(value, index) { 
    30       result = result && !!iterator(value, index); 
     28      result = result && !!iterator.call(context, value, index); 
    3129      if (!result) throw $break; 
    3230    }); 
     
    3533 
    3634  any: function(iterator, context) { 
    37     iterator = iterator ? iterator.bind(context) : Prototype.K; 
     35    iterator = iterator || Prototype.K; 
    3836    var result = false; 
    3937    this.each(function(value, index) { 
    40       if (result = !!iterator(value, index)) 
     38      if (result = !!iterator.call(context, value, index)) 
    4139        throw $break; 
    4240    }); 
     
    4543 
    4644  collect: function(iterator, context) { 
    47     iterator = iterator ? iterator.bind(context) : Prototype.K; 
    48     var results = []; 
    49     this.each(function(value, index) { 
    50       results.push(iterator(value, index)); 
     45    iterator = iterator || Prototype.K; 
     46    var results = []; 
     47    this.each(function(value, index) { 
     48      results.push(iterator.call(context, value, index)); 
    5149    }); 
    5250    return results; 
     
    5452   
    5553  detect: function(iterator, context) { 
    56     iterator = iterator.bind(context); 
    5754    var result; 
    5855    this.each(function(value, index) { 
    59       if (iterator(value, index)) { 
     56      if (iterator.call(context, value, index)) { 
    6057        result = value; 
    6158        throw $break; 
     
    6663   
    6764  findAll: function(iterator, context) { 
    68     iterator = iterator.bind(context); 
    69     var results = []; 
    70     this.each(function(value, index) { 
    71       if (iterator(value, index)) 
     65    var results = []; 
     66    this.each(function(value, index) { 
     67      if (iterator.call(context, value, index)) 
    7268        results.push(value); 
    7369    }); 
     
    7672   
    7773  grep: function(filter, iterator, context) { 
    78     iterator = iterator ? iterator.bind(context) : Prototype.K; 
     74    iterator = iterator || Prototype.K; 
    7975    var results = []; 
    8076 
     
    8480    this.each(function(value, index) { 
    8581      if (filter.match(value)) 
    86         results.push(iterator(value, index)); 
     82        results.push(iterator.call(context, value, index)); 
    8783    }); 
    8884    return results; 
     
    112108   
    113109  inject: function(memo, iterator, context) { 
    114     iterator = iterator.bind(context); 
    115     this.each(function(value, index) { 
    116       memo = iterator(memo, value, index); 
     110    this.each(function(value, index) { 
     111      memo = iterator.call(context, memo, value, index); 
    117112    }); 
    118113    return memo; 
     
    127122   
    128123  max: function(iterator, context) { 
    129     iterator = iterator ? iterator.bind(context) : Prototype.K; 
     124    iterator = iterator || Prototype.K; 
    130125    var result; 
    131126    this.each(function(value, index) { 
    132       value = iterator(value, index); 
     127      value = iterator.call(context, value, index); 
    133128      if (result == null || value >= result) 
    134129        result = value; 
     
    138133   
    139134  min: function(iterator, context) { 
    140     iterator = iterator ? iterator.bind(context) : Prototype.K; 
     135    iterator = iterator || Prototype.K; 
    141136    var result; 
    142137    this.each(function(value, index) { 
    143       value = iterator(value, index); 
     138      value = iterator.call(context, value, index); 
    144139      if (result == null || value < result) 
    145140        result = value; 
     
    149144   
    150145  partition: function(iterator, context) { 
    151     iterator = iterator ? iterator.bind(context) : Prototype.K; 
     146    iterator = iterator || Prototype.K; 
    152147    var trues = [], falses = []; 
    153148    this.each(function(value, index) { 
    154       (iterator(value, index) ?  
     149      (iterator.call(context, value, index) ? 
    155150        trues : falses).push(value); 
    156151    }); 
     
    167162   
    168163  reject: function(iterator, context) { 
    169     iterator = iterator.bind(context); 
    170     var results = []; 
    171     this.each(function(value, index) { 
    172       if (!iterator(value, index)) 
     164    var results = []; 
     165    this.each(function(value, index) { 
     166      if (!iterator.call(context, value, index)) 
    173167        results.push(value); 
    174168    }); 
     
    177171   
    178172  sortBy: function(iterator, context) { 
    179     iterator = iterator.bind(context); 
    180173    return this.map(function(value, index) { 
    181       return {value: value, criteria: iterator(value, index)}; 
     174      return { 
     175        value: value, 
     176        criteria: iterator.call(context, value, index) 
     177      }; 
    182178    }).sort(function(left, right) { 
    183179      var a = left.criteria, b = right.criteria;