Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Using the push method or .length when adding to array? #1025

Closed
stonelee opened this Issue · 2 comments

4 participants

@stonelee

When I read the source file, I found that when adding to array, there are two different ways.

  _.keys = nativeKeys || function(obj) {
    if (obj !== Object(obj)) throw new TypeError('Invalid object');
    var keys = [];
    for (var key in obj) if (_.has(obj, key)) keys[keys.length] = key;
    return keys;
  };

  // Retrieve the values of an object's properties.
  _.values = function(obj) {
    var values = [];
    for (var key in obj) if (_.has(obj, key)) values.push(obj[key]);
    return values;
  };

  // Convert an object into a list of `[key, value]` pairs.
  _.pairs = function(obj) {
    var pairs = [];
    for (var key in obj) if (_.has(obj, key)) pairs.push([key, obj[key]]);
    return pairs;
  };

I try to find the best way, and found
http://stackoverflow.com/questions/6772757/using-the-push-method-or-length-when-adding-to-array

I think in different javascript engine, speed is different.
Readability and unification is more import.
so we'd better to unify the two ways

@stonelee stonelee referenced this issue from a commit in stonelee/underscore
@stonelee stonelee unify the way to adding to array.fix Issue #1025 ee24bbc
@braddunbar
Collaborator

:clap: Much easier to read.

@havvg havvg referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@havvg havvg referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@sandrasl

help full and easy. i use for my site http://thepushhub.com/ as well.

@jashkenas jashkenas closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.