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

Changeset 7861

Show
Ignore:
Timestamp:
10/13/07 19:05:19 (1 year ago)
Author:
tobie
Message:

Performance improvements to String#times.

Files:

Legend:

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

    r7855 r7861  
    11*SVN* 
     2 
     3* Performance improvements to String#times. [Martin Ström] 
    24 
    35* Make Ajax.Response#getHeaderJSON and Ajax.Response#getResponseJSON pseudo private instance methods. [Tobie Langel] 
  • spinoffs/prototype/trunk/src/string.js

    r7344 r7861  
    120120   
    121121  times: function(count) { 
    122     var result = ''; 
    123     for (var i = 0; i < count; i++) result += this; 
    124     return result; 
     122    return count < 1 ? '' : new Array(count + 1).join(this); 
    125123  }, 
    126124   
  • spinoffs/prototype/trunk/test/unit/string.html

    r7221 r7861  
    464464 
    465465    testTimes: function() {with(this) { 
     466 
    466467      assertEqual('', ''.times(0)); 
    467468      assertEqual('', ''.times(5)); 
     469      assertEqual('', 'a'.times(-1)); 
    468470      assertEqual('', 'a'.times(0)); 
    469471      assertEqual('a', 'a'.times(1)); 
     472      assertEqual('aa', 'a'.times(2)); 
    470473      assertEqual('aaaaa', 'a'.times(5)); 
    471474      assertEqual('foofoofoofoofoo', 'foo'.times(5)); 
    472475      assertEqual('', 'foo'.times(-5)); 
     476       
     477      /*window.String.prototype.oldTimes = function(count) { 
     478        var result = ''; 
     479        for (var i = 0; i < count; i++) result += this; 
     480        return result; 
     481      }; 
     482       
     483      benchmark(function() { 
     484        'foo'.times(15); 
     485      }, 1000, 'new: '); 
     486       
     487      benchmark(function() { 
     488        'foo'.oldTimes(15); 
     489      }, 1000, 'previous: ');*/ 
    473490    }}, 
    474491