Skip to content

Instantly share code, notes, and snippets.

@dandean
Created October 23, 2012 06:52
Show Gist options
  • Select an option

  • Save dandean/3937303 to your computer and use it in GitHub Desktop.

Select an option

Save dandean/3937303 to your computer and use it in GitHub Desktop.

What is the best way to inject a value into the middle of arguments?

  1. Would prefer it to be a one liner
  2. Need it to be really fast!

JSPerf here: http://jsperf.com/inject-into-arguments

Of the two below "slice-and-concat.js" is 12% faster in Chrome.

function fn() {
return [arguments[0],'INJECTED VALUE'].concat(Array.prototype.slice.call(arguments, 1));
}
fn(1, 2, 3, 4);
function fn() {
var args = Array.prototype.slice.call(arguments);
args.splice(1,0,'INJECTED VALUE');
return args;
}
fn(1, 2, 3, 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment