Loading ...
Sorry, an error occurred while loading the content.

Use of a.push()

Expand Messages
  • ingliss
    Is there any reason why this is used: function e(s) { a[a.length] = s; } rather than function e(s) { a.push(s); } ? I don t suppose it makes much difference in
    Message 1 of 3 , Jan 3, 2006
    • 0 Attachment
      Is there any reason why this is used:

      function e(s) {
      a[a.length] = s;
      }

      rather than

      function e(s) {
      a.push(s);
      }

      ?

      I don't suppose it makes much difference in terms of performance?

      In a similar vein, the code for stringing together a representation of
      an array takes the route of testing to see if the item being added is
      the last in the list, before deciding whether or not to add a ",".

      Would there be a benefit in always appending the ",", and then using
      a.pop() remove the excess "," character outside the loop?

      I guess it depends on how expensive a single pop() is compared to the
      multiple tests.


      Finally, I've had a good look at the conditions of use and my question
      is: how can I tell if I'm evil or not?
    • Douglas Crockford
      ... This question is way off topic for this group. This group is about JSON, not JavaScript coding. But to answer your question, the first form is slightly
      Message 2 of 3 , Jan 3, 2006
      • 0 Attachment
        > Is there any reason why this is used:
        >
        > function e(s) {
        > a[a.length] = s;
        > }
        >
        > rather than
        >
        > function e(s) {
        > a.push(s);
        > }

        This question is way off topic for this group. This group is about
        JSON, not JavaScript coding.

        But to answer your question, the first form is slightly faster and
        will run on older language processors.
      • ingliss
        ... Hmmm. Well, if you say so, but I d rather taken my lead as to relevance from the post where you discuss Javascript efficiency in the parse function :-) ...
        Message 3 of 3 , Jan 3, 2006
        • 0 Attachment
          --- In json@yahoogroups.com, "Douglas Crockford" <douglas@c...> wrote:
          >
          > > Is there any reason why this is used:
          > >
          > > function e(s) {
          > > a[a.length] = s;
          > > }
          > >
          > > rather than
          > >
          > > function e(s) {
          > > a.push(s);
          > > }
          >
          > This question is way off topic for this group. This group is about
          > JSON, not JavaScript coding.
          >


          Hmmm.

          Well, if you say so, but I'd rather taken my lead as to relevance from
          the post where you discuss Javascript efficiency in the parse
          function :-)


          > But to answer your question, the first form is slightly faster and
          > will run on older language processors.
          >

          Ok, I'd have guessed it would be slower.
        Your message has been successfully submitted and would be delivered to recipients shortly.