Array push/pop JavaScript tests

This page allows you to test various implementations of Array.prototype.push and Array.prototype.pop. The implementations were selected from high-ranking Google results for push and pop.

The tests expose the fact that (at the time of writing) no browser implements arrays 100% correctly. If your browser passes all the tests, please let me know by commenting on my push and pop blog entry.

OriginTestSummary
Browser Test the native implementation built-in to your browser
Ash Searle Fully compliant implementation (only fails tests that depend on perfect array implementation.)

		Array.prototype.push = function() {
		    var n = this.length >>> 0;
		    for (var i = 0; i < arguments.length; i++) {
			this[n] = arguments[i];
			n = n + 1 >>> 0;
		    }
		    this.length = n;
		    return n;
		};

		Array.prototype.pop = function() {
		    var n = this.length >>> 0, value;
		    if (n) {
			value = this[--n];
			delete this[n];
		    }
		    this.length = n;
		    return value;
		};
		
view source…
Unknown Common/trivial implementation seen on sites such as WebMonkey, JavaScript Method Library, LunaMetrics and many others...

		Array.prototype.push = function() {
		    for (var i = 0; i != arguments.length; i++) {
			this[this.length] = arguments[i];
		    }
		    return this.length;
		};

		Array.prototype.pop = function() {
		    var b = this[this.length - 1];
		    this.length--;
		    return b
		}; 
		
view source…
FAQTs Example push/pop implementations at FAQTs (original source: WebReference). Note: pop polutes global-scope with lastElement (easily fixed with an appropriate var)

		Array.prototype.push = function() {
		    for (var i = 0; i < arguments.length; i++) {
			this[this.length] = arguments[i];
		    };
		    return this.length;
		};

		Array.prototype.pop = function() {
		    lastElement = this[this.length - 1];
		    this.length = Math.max(this.length - 1, 0);
		    return lastElement;
		};
		
view source…
Douglas Crockford Doug Crockford's implementation depends on a reusable splice and slice Array methods. This can hang on sparse-arrays (i.e. the test may hang on Internet Explorer)

		Array.prototype.push = function() {
		    this.splice.apply(this,
			    [this.length, 0].concat(Array.prototype.slice.apply(arguments)));
		    return this.length;
		};

		Array.prototype.pop = function() {
		    return this.splice(this.length - 1, 1)[0];
		};
		
view source…