Skip to content

Commit cbc7a40

Browse files
fix: reorder Best Practices
1 parent 8e7a6ed commit cbc7a40

File tree

1 file changed

+51
-51
lines changed

1 file changed

+51
-51
lines changed

data/blog/software-development/web-development/frontend/javascript/slice-vs-substring-vs-substr-complete-javascript-string-methods-comparison.mdx

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,57 +1066,6 @@ console.log(str.slice(4, 6)); // "Sc" (from index 4 to 6, exclusive)
10661066
// Better: Use slice() instead of substr()
10671067
```
10681068

1069-
## Performance Considerations
1070-
1071-
While all three methods have similar performance characteristics, `slice()` is generally preferred because:
1072-
1073-
1. **Modern Standard**: Part of ECMAScript standard
1074-
2. **Consistent Behavior**: Predictable and intuitive
1075-
3. **Array Compatibility**: Works similarly to `Array.slice()`, reducing cognitive load
1076-
4. **Future-Proof**: Not deprecated, actively maintained
1077-
1078-
```javascript
1079-
// Performance is similar for all methods
1080-
const str = "a".repeat(1000000);
1081-
1082-
console.time('slice');
1083-
for (let i = 0; i < 10000; i++) {
1084-
str.slice(100, 200);
1085-
}
1086-
console.timeEnd('slice');
1087-
1088-
console.time('substring');
1089-
for (let i = 0; i < 10000; i++) {
1090-
str.substring(100, 200);
1091-
}
1092-
console.timeEnd('substring');
1093-
1094-
console.time('substr');
1095-
for (let i = 0; i < 10000; i++) {
1096-
str.substr(100, 100);
1097-
}
1098-
console.timeEnd('substr');
1099-
1100-
// In most cases, performance difference is negligible
1101-
// Choose based on functionality, not performance
1102-
```
1103-
1104-
## Browser Compatibility
1105-
1106-
All three methods are widely supported:
1107-
1108-
- **`slice()`**: Supported in all modern browsers (IE4+), part of ECMAScript 3 standard
1109-
- **`substring()`**: Supported in all modern browsers (IE3+), part of ECMAScript 1 standard
1110-
- **`substr()`**: Supported but deprecated (IE3+, but should not be used), not part of ECMAScript standard
1111-
1112-
For modern JavaScript development, `slice()` is the recommended choice as it's part of the ECMAScript standard and has the most intuitive behavior.
1113-
1114-
## Further Learning Resources
1115-
1116-
Mastering JavaScript string methods is just one piece of becoming a proficient JavaScript developer. If you're looking to deepen your JavaScript knowledge and build real-world applications, there are excellent learning resources available.
1117-
1118-
For developers who prefer interactive, text-based learning over video tutorials, the [Complete JavaScript Course: Build a Real World App from Scratch](https://www.educative.io/courses/the-complete-javascript-course-build-a-real-world-app-from-scratch?aff=VALz) on Educative offers a video-free, hands-on approach with embedded code editors and immediate feedback. If you prefer video-based learning from industry experts, the [Programming with JavaScript](https://imp.i384100.net/YRXbYJ) course by Meta on Coursera provides structured video lectures as part of Meta's Front-End Developer Professional Certificate program.
1119-
11201069
## Best Practices
11211070

11221071
### 1. Prefer slice() Over substring() and substr()
@@ -1186,6 +1135,57 @@ function processStringInconsistent(str) {
11861135
}
11871136
```
11881137

1138+
## Performance Considerations
1139+
1140+
While all three methods have similar performance characteristics, `slice()` is generally preferred because:
1141+
1142+
1. **Modern Standard**: Part of ECMAScript standard
1143+
2. **Consistent Behavior**: Predictable and intuitive
1144+
3. **Array Compatibility**: Works similarly to `Array.slice()`, reducing cognitive load
1145+
4. **Future-Proof**: Not deprecated, actively maintained
1146+
1147+
```javascript
1148+
// Performance is similar for all methods
1149+
const str = "a".repeat(1000000);
1150+
1151+
console.time('slice');
1152+
for (let i = 0; i < 10000; i++) {
1153+
str.slice(100, 200);
1154+
}
1155+
console.timeEnd('slice');
1156+
1157+
console.time('substring');
1158+
for (let i = 0; i < 10000; i++) {
1159+
str.substring(100, 200);
1160+
}
1161+
console.timeEnd('substring');
1162+
1163+
console.time('substr');
1164+
for (let i = 0; i < 10000; i++) {
1165+
str.substr(100, 100);
1166+
}
1167+
console.timeEnd('substr');
1168+
1169+
// In most cases, performance difference is negligible
1170+
// Choose based on functionality, not performance
1171+
```
1172+
1173+
## Browser Compatibility
1174+
1175+
All three methods are widely supported:
1176+
1177+
- **`slice()`**: Supported in all modern browsers (IE4+), part of ECMAScript 3 standard
1178+
- **`substring()`**: Supported in all modern browsers (IE3+), part of ECMAScript 1 standard
1179+
- **`substr()`**: Supported but deprecated (IE3+, but should not be used), not part of ECMAScript standard
1180+
1181+
For modern JavaScript development, `slice()` is the recommended choice as it's part of the ECMAScript standard and has the most intuitive behavior.
1182+
1183+
## Further Learning Resources
1184+
1185+
Mastering JavaScript string methods is just one piece of becoming a proficient JavaScript developer. If you're looking to deepen your JavaScript knowledge and build real-world applications, there are excellent learning resources available.
1186+
1187+
For developers who prefer interactive, text-based learning over video tutorials, the [Complete JavaScript Course: Build a Real World App from Scratch](https://www.educative.io/courses/the-complete-javascript-course-build-a-real-world-app-from-scratch?aff=VALz) on Educative offers a video-free, hands-on approach with embedded code editors and immediate feedback. If you prefer video-based learning from industry experts, the [Programming with JavaScript](https://imp.i384100.net/YRXbYJ) course by Meta on Coursera provides structured video lectures as part of Meta's Front-End Developer Professional Certificate program.
1188+
11891189
## Conclusion
11901190

11911191
Understanding the differences between `String.prototype.slice()`, `String.prototype.substring()`, and `String.prototype.substr()` (commonly used as `slice()`, `substring()`, and `substr()`) is crucial for writing clean, maintainable JavaScript code. Throughout this comprehensive guide, we've explored the intricate behaviors of each method, their use cases, pitfalls, and best practices.

0 commit comments

Comments
 (0)