#258 enhancement
Marton Kiss-Albert

Object._each() optimization

Reported by Marton Kiss-Albert | July 30th, 2008 @ 10:10 PM | in 1.7

// OLD VERSION:
  _each: function(iterator) {
    for (var i = 0, length = this.length; i < length; i++)
      iterator(this[i]);
  },
  
// NEW VERSION:
  _each: function(iterator) {
    for (var i = 0, length = this.length; i < length; iterator(this[i++]));
  },

Comments and changes to this ticket

  • John-David Dalton

    John-David Dalton July 31st, 2008 @ 12:06 AM

    what about:

    _each: function(iterator) {
      for (var i = 0, item; item = this[i] ; i++)
        iterator(item);
    },
    

    OR

    _each: function(iterator) {
      for (var i = 0, item; item = this[i++];)
        iterator(item);
     },
    

    Can you provide simple benchmarks for each of these so we may compare the performance.

  • John-David Dalton

    John-David Dalton July 31st, 2008 @ 12:09 AM

    OR even

    _each: function(iterator) {
      for (var i = 0, item; item = this[i++]; iterator(item));
    },
    
  • John-David Dalton

    John-David Dalton July 31st, 2008 @ 12:10 AM

    • Assigned user set to “John-David Dalton”
    • State changed from “new” to “enhancement”
    • Tag changed from needs_benchmarks, optimization to needs_benchmarks, needs_patch, needs_tests, optimization
    • Milestone set to 1.7
  • Juriy Zaytsev

    Juriy Zaytsev July 31st, 2008 @ 06:55 AM

    Interesting. Definitely need benchmarks for this.

  • Tobie Langel

    Tobie Langel July 23rd, 2009 @ 04:08 AM

    • Tag changed from needs_benchmarks, needs_patch, needs_tests, optimization to needs_benchmarks, needs_patch, needs_tests, performance
    • Assigned user cleared.
  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:26 AM

    • Tag changed from needs_benchmarks, needs_patch, needs_tests, performance to missing:tests, needs_benchmarks, needs_patch, performance

    [not-tagged:"needs_tests" tagged:"missing:tests" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:27 AM

    • Tag changed from missing:tests, needs_benchmarks, needs_patch, performance to missing:benchmarks, missing:tests, needs_patch, performance

    [not-tagged:"needs_benchmarks" tagged:"missing:benchmarks" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:28 AM

    • Tag changed from missing:benchmarks, missing:tests, needs_patch, performance to missing:benchmarks, missing:patch, missing:tests, performance

    [not-tagged:"needs_patch" tagged:"missing:patch" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:36 AM

    • Tag changed from missing:benchmarks, missing:patch, missing:tests, performance to missing:benchmarks, missing:patch, needs:tests, performance

    [not-tagged:"missing:tests" tagged:"needs:tests" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:37 AM

    • Tag changed from missing:benchmarks, missing:patch, needs:tests, performance to missing:benchmarks, needs:patch, needs:tests, performance

    [not-tagged:"missing:patch" tagged:"needs:patch" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:39 AM

    • Tag changed from missing:benchmarks, needs:patch, needs:tests, performance to needs:benchmarks, needs:patch, needs:tests, performance

    [not-tagged:"missing:benchmarks" tagged:"needs:benchmarks" bulk edit command]

  • T.J. Crowder

    T.J. Crowder November 16th, 2009 @ 04:50 PM

    [responsible:none bulk edit command]

  • Radoslav Stankov

    Radoslav Stankov March 2nd, 2010 @ 09:49 AM

    • Tag changed from needs:benchmarks, needs:patch, needs:tests, performance to needs:benchmarks, needs:patch, needs:tests, performance, section:lang
  • Arthur Schreiber

    Arthur Schreiber March 11th, 2010 @ 09:21 AM

    • Tag changed from needs:benchmarks, needs:patch, needs:tests, performance, section:lang to benchmarked, needs:patch, needs:tests, performance, section:lang
    • Assigned user set to “Juriy Zaytsev”

    I created some benchmarks and results for different browsers: http://gist.github.com/328956
    Basically, the best-performing version was the one proposed by Marton.

  • Arthur Schreiber

    Arthur Schreiber March 11th, 2010 @ 09:35 AM

    Interesting thing to note is, that in all browsers except Firefox that implement a native Array#forEach, the native version is slower than the one proposed by Marton.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

Pages