Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Improve performance findLast by returning immediately #20

Merged
merged 1 commit into from

2 participants

@toonketels

Changed findLast to start from the end of the array, working its way back, returning as soon as it has found the value.

The previous implementation would always enumerate the entire array.

@tj
Owner
tj commented

good call, thanks!

@tj tj merged commit 737638c into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 17, 2013
  1. @toonketels

    Changed findLast to start from the end of the array, working its way …

    toonketels authored
    …back, returning as soon as it has found the value.
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 4 deletions.
  1. +2 −4 index.js
View
6 index.js
@@ -289,15 +289,13 @@ proto.find = function(fn){
proto.findLast = function(fn){
fn = toFunction(fn);
- var ret;
var val;
var vals = this.__iterate__();
var len = vals.length();
- for (var i = 0; i < len; ++i) {
+ for (var i = len - 1; i > -1; --i) {
val = vals.get(i);
- if (fn(val, i)) ret = val;
+ if (fn(val, i)) return val;
}
- return ret;
};
/**
Something went wrong with that request. Please try again.