Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
42 events
when toggle format what by license comment
Jun 7, 2024 at 9:37 comment added Hun toSpliced is works well!
May 24, 2024 at 3:38 history edited Advait Junnarkar CC BY-SA 4.0
Incorporate other answers to reflect an updated API and a more modern approach.
Oct 5, 2023 at 15:32 comment added Daniel @NateThompson When the index doesn't exist (i.e. it needs to be inserted between two values, not in place of a value).
Aug 13, 2021 at 13:57 history edited Peter Mortensen CC BY-SA 4.0
Brevity.
May 13, 2021 at 19:36 comment added Nate Thompson Is there a certain critereria that you would you splice instead of just array[index] = newValue?
Apr 22, 2021 at 3:33 history edited etoxin CC BY-SA 4.0
Added log output
S Dec 20, 2020 at 16:19 history suggested Gabor CC BY-SA 4.0
The link was dead, just updated with a live one
Dec 20, 2020 at 14:57 review Suggested edits
S Dec 20, 2020 at 16:19
Dec 16, 2020 at 3:41 history edited d-_-b CC BY-SA 4.0
added 2 characters in body
Nov 2, 2020 at 10:48 comment added Bitterblue Splice was premature optimization, when JavaScript got "invented". Instead of just throwing in the common "insert/remove", they took the splice function, that does both and pains every newcomer. Now everybody is used to splice and will disagree with me.
May 1, 2018 at 14:42 review Suggested edits
May 1, 2018 at 14:46
Apr 24, 2018 at 11:03 history edited Zze CC BY-SA 3.0
Created a snippet
May 1, 2017 at 18:41 review Suggested edits
May 1, 2017 at 20:45
Sep 23, 2016 at 17:55 comment added tetris11 given that the new class and constructor elements for the new EMCAScript is nothing more than syntactic sugar for existing methods, would it be too much to make an insert function/alias ?
S Jul 14, 2016 at 20:14 history edited tvanfosson CC BY-SA 3.0
Added explanation to what the 0 parameter is
S Jul 14, 2016 at 20:14 history suggested Jeremy W CC BY-SA 3.0
Added explanation to what the 0 parameter is
Jul 14, 2016 at 20:10 review Suggested edits
S Jul 14, 2016 at 20:14
Jun 3, 2016 at 12:19 comment added ArtOfWarfare @tonix - Your code is written in JavaScript and needs to be interpreted. Splice's code is likely more native.
Jun 3, 2016 at 6:59 comment added tonix @ArtOfWarfare Of course, I have to benchmark it before I can make assumptions about a performance gain, but with such an implementation the amount of required operations is constant. It doesn't matter how long the list is. I remember there was an online site where you can benchmark you JS code, but I couldn't remember the name. Could you pass me a link if you know it. I will then benchmark the code I have written against splice and tell what the results are.
Jun 3, 2016 at 6:57 comment added tonix @ArtOfWarfare As tvanfosson said, the amount of operations of splice is O(n). Meanwhile, I have created a class which I call a LinkedOrderedMap, which allows me to insert items and keep them in order, and when I need to remove an item from the list I simply unlink the node where the item is located inside the list and tell the previous node that its next item now is the next node of the node I am going to remove. To insert an element between other elements in the list, I can use the same strategy, too.
Jun 1, 2016 at 2:36 comment added ArtOfWarfare @tonix - Do you have an actual performance problem? If not, you're doing premature optimization, which leads to harder to maintain code with no benefit. Making splice perform well is the responsibility of the person writing the Javascript engine/library, not yours. Don't make it yours unless you have to (IE, your program is unacceptably slow unless you have some ugly hacks.)
Oct 24, 2015 at 22:33 comment added tvanfosson I think he's talking about techniques to avoid resizing the array by keeping track of the the valid indices yourself. Unless you're facing a performance issue currently I wouldn't start trying to write your own array options. It'ls also possible that another data structure might work better - say balanced trees - depending on the need of your application.
Oct 24, 2015 at 22:23 comment added tonix I understand. Can it be improved somehow? I mean, I have just found something like creating two separate indexes placed in the middle of the array or rotating arrays but I didn't understand what the author of the article meant (gamealchemist.wordpress.com/2013/05/01/…), point 6 at the end and 7
Oct 24, 2015 at 22:17 comment added tvanfosson @tonix I would think that it would be an O(n) operation as it has to shift all the values belonging to the indices following the items inserted. In the worst case (inserting an item at the beginning) that would be all of the (n) items in the array.
Oct 24, 2015 at 20:56 comment added tonix Could someone please tell me from a performance point of view is using splice good in this case? I mean, if I have an array with thousands of items and I want to insert an element somewhere between two elements, how does JavaScript update the indexes of the array from that point where I insert the item to the end of the array?
S May 26, 2015 at 9:15 history suggested Mikepote CC BY-SA 3.0
Added a usage sentence to explain the meaning of the parameters passed to slice.
May 26, 2015 at 8:29 review Suggested edits
S May 26, 2015 at 9:15
Nov 21, 2014 at 15:45 comment added Jakub Keller I think the term "splice" makes sense. Splice means to join or connect, also to change. You have an established array that you are now "changing" which would involve adding or removing elements. You specify where in the array to start, then how many old items to remove (if any) and lastly, optionally a list of new elements to add. Splice is also a great sci-fi term of course.
May 13, 2014 at 1:45 comment added EBarr Splice can insert, but just as frequently does not. For example: arr.splice(2,3) will remove 3 elements starting at index 2. Without passing the 3rd....Nth parameters nothing is inserted. So the name insert() doesn't do it justice either.
Apr 12, 2014 at 0:29 history edited user142162 CC BY-SA 3.0
deleted 7 characters in body
Feb 22, 2014 at 19:02 history edited tvanfosson CC BY-SA 3.0
[Edit removed during grace period]
Feb 22, 2014 at 18:06 review Suggested edits
Feb 22, 2014 at 18:08
Dec 3, 2013 at 1:19 history edited senfo CC BY-SA 3.0
Added slightly more information about where the element will be inserted.
Mar 26, 2013 at 18:36 review Suggested edits
Mar 26, 2013 at 18:39
Oct 10, 2012 at 21:49 history edited root-aj CC BY-SA 3.0
replaced `document.write` (deprecated) and `new Array(n)` (doesn't behave as expected) and script tag (doesn't run in NodeJS)
Jan 7, 2012 at 6:45 history edited Prestaul CC BY-SA 3.0
improved formatting (things that aren't quotes shouldn't be quoted...)
Nov 20, 2011 at 9:49 history edited jcolebrand CC BY-SA 3.0
updated with a MDN instead of w3schools reference, as it's considered more canonical (and more in depth)
Mar 10, 2011 at 9:54 comment added Dingo doc: developer.mozilla.org/en/JavaScript/Guide/…
Feb 25, 2009 at 14:53 comment added Christoph @tags2k: because the function does more than inserting items and it's name was already established in perl?
Feb 25, 2009 at 14:46 comment added tags2k Thanks, I thought I would feel stupid for asking but now that I know the answer I don't! Why on earth did they decide to call it splice when a more searchable term was in common use for the same function?!
Feb 25, 2009 at 14:44 vote accept tags2k
Feb 25, 2009 at 14:32 history answered tvanfosson CC BY-SA 2.5