Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

#Fast strings in Javascript

Fast strings in Javascript

It is important to note that something as simple as assigning a string to a variable will cost the length of the string in complexity a = "12345" will cost 5 characters. Assign to another variable and the cost is the same b = a will cost 5.

Every time you assign a string you need to count the length of that string. If you copy an array of strings then you move all the characters, not just the number of strings in the array.

You do a lot of string assignments in your code.

##Avoid string assignment

Avoid string assignment

The trick to handling strings is to avoid assigning strings if you can. Arrays and string array character referencing (eg a = "abcdef"; b = a[1]) lets you treat the JS string like a C (char *) string. You dont copy the string, you are just indexing into the string.

The only time any sequence of characters are copied is on the return. The performance increase is very significant.

const longestCommonPrefix = words => {
 var i, pos = 0, len = words.length;
 const char = words[0][pos];
 var min = words[0].length;
 for (i = 1; i < len; i++) { // finds the min word length and check first char
 min = words[i].length < min ? words[i].length : min;
 if (char !== words[i][pos] || pos === min) { return "" }
 } 
 pos ++;
 while (pos < min) { // check up to min word length.
 const char = words[0][pos];
 for (i = 1; i <len; i++) {
 if (char !== words[i][pos]) { return words[0].substring(0,pos) }
 }
 pos ++;
 }
 return words[0].substring(0,pos);
}

#Fast strings in Javascript

It is important to note that something as simple as assigning a string to a variable will cost the length of the string in complexity a = "12345" will cost 5 characters. Assign to another variable and the cost is the same b = a will cost 5.

Every time you assign a string you need to count the length of that string. If you copy an array of strings then you move all the characters, not just the number of strings in the array.

You do a lot of string assignments in your code.

##Avoid string assignment

The trick to handling strings is to avoid assigning strings if you can. Arrays and string array character referencing (eg a = "abcdef"; b = a[1]) lets you treat the JS string like a C (char *) string. You dont copy the string, you are just indexing into the string.

The only time any sequence of characters are copied is on the return. The performance increase is very significant.

const longestCommonPrefix = words => {
 var i, pos = 0, len = words.length;
 const char = words[0][pos];
 var min = words[0].length;
 for (i = 1; i < len; i++) { // finds the min word length and check first char
 min = words[i].length < min ? words[i].length : min;
 if (char !== words[i][pos] || pos === min) { return "" }
 } 
 pos ++;
 while (pos < min) { // check up to min word length.
 const char = words[0][pos];
 for (i = 1; i <len; i++) {
 if (char !== words[i][pos]) { return words[0].substring(0,pos) }
 }
 pos ++;
 }
 return words[0].substring(0,pos);
}

Fast strings in Javascript

It is important to note that something as simple as assigning a string to a variable will cost the length of the string in complexity a = "12345" will cost 5 characters. Assign to another variable and the cost is the same b = a will cost 5.

Every time you assign a string you need to count the length of that string. If you copy an array of strings then you move all the characters, not just the number of strings in the array.

You do a lot of string assignments in your code.

Avoid string assignment

The trick to handling strings is to avoid assigning strings if you can. Arrays and string array character referencing (eg a = "abcdef"; b = a[1]) lets you treat the JS string like a C (char *) string. You dont copy the string, you are just indexing into the string.

The only time any sequence of characters are copied is on the return. The performance increase is very significant.

const longestCommonPrefix = words => {
 var i, pos = 0, len = words.length;
 const char = words[0][pos];
 var min = words[0].length;
 for (i = 1; i < len; i++) { // finds the min word length and check first char
 min = words[i].length < min ? words[i].length : min;
 if (char !== words[i][pos] || pos === min) { return "" }
 } 
 pos ++;
 while (pos < min) { // check up to min word length.
 const char = words[0][pos];
 for (i = 1; i <len; i++) {
 if (char !== words[i][pos]) { return words[0].substring(0,pos) }
 }
 pos ++;
 }
 return words[0].substring(0,pos);
}
Source Link
Blindman67
  • 22.8k
  • 2
  • 16
  • 40

#Fast strings in Javascript

It is important to note that something as simple as assigning a string to a variable will cost the length of the string in complexity a = "12345" will cost 5 characters. Assign to another variable and the cost is the same b = a will cost 5.

Every time you assign a string you need to count the length of that string. If you copy an array of strings then you move all the characters, not just the number of strings in the array.

You do a lot of string assignments in your code.

##Avoid string assignment

The trick to handling strings is to avoid assigning strings if you can. Arrays and string array character referencing (eg a = "abcdef"; b = a[1]) lets you treat the JS string like a C (char *) string. You dont copy the string, you are just indexing into the string.

The only time any sequence of characters are copied is on the return. The performance increase is very significant.

const longestCommonPrefix = words => {
 var i, pos = 0, len = words.length;
 const char = words[0][pos];
 var min = words[0].length;
 for (i = 1; i < len; i++) { // finds the min word length and check first char
 min = words[i].length < min ? words[i].length : min;
 if (char !== words[i][pos] || pos === min) { return "" }
 } 
 pos ++;
 while (pos < min) { // check up to min word length.
 const char = words[0][pos];
 for (i = 1; i <len; i++) {
 if (char !== words[i][pos]) { return words[0].substring(0,pos) }
 }
 pos ++;
 }
 return words[0].substring(0,pos);
}
default

AltStyle によって変換されたページ (->オリジナル) /