Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

time complexity of slice javascript

Array.prototype.slice is O(n), where n is the number of elements in the slice.

String.prototype.slice is O(1) time complexity

//example

let nested = {name: "test"};
var a = [nested];
console.log(a)
var b = a.slice(0, 1);
console.log(b)
a[0].name = "abc";
console.log(a === b);          // false, `b` is a copy
console.log(a[0] === b[0]);    // true, `nested` was not copied
console.log(b[0] === nested);  // true
console.log(b[0].name);
 
PREVIOUS NEXT
Tagged: #time #complexity #slice #javascript
ADD COMMENT
Topic
Name
5+9 =