1

I have the following:

var students = [{name:"Jordan", age:"6"},{name:"Jake", age:"7"},{name:"Mark", age:"10"}]

I want to generate a string like this: "Jordan,6|Jake,7|Mark,10"

What is the most efficient way to do this?

I am currently using:

var studentstr = "";
for(var i = 0; i < students.length; i++) {
  studentstr = students['name'] + "," + students['age'] + "|"
}
studentstr = studentstr.substring(0, studentstr.length - 1);

Also, performance-wise, if I had an array of 2,000 items, is it "costly" to perform this transformation? The resulting string contains both keys in the object and not a single join on one object in the property.

1

4 Answers 4

2

You can map each student object to a string and then join them all with |:

var studentstr = students.map(function (student) {
    return student.name + ',' + student.age;
}).join('|');

Also, performance-wise, if I had an array of 2,000 items, is it "costly" to perform this transformation?

No.

Sign up to request clarification or add additional context in comments.

2 Comments

Does anyone know if this performs better or worse than Guffa's answer? Or is .map effectively doing the same thing under the hood?
@Setsuna: .map is doing the same thing under the hood, but it will likely perform “worse” because of the function calls. With 2,000 items, however, you won’t notice any difference, and your original version is probably best in that regard (except for the final substring).
1

Yes, using string concatenation in a loop is costly. The string grows for each iteration, and each time you have to copy the entire previous string to create the new version. The execution time of the loop grows exponentially to the number of items.

You can put the string for each object in an array, then join them together:

var students = [{name:"Jordan", age:"6"},{name:"Jake", age:"7"},{name:"Mark", age:"10"}];

var items = [];
for (var i = 0; i < students.length; i++) {
  items.push(students[i].name + ',' +students[i].age);
}
var str = items.join('|');

// display result in snippet
document.write(str);

5 Comments

“Yes, using string concatenation in a loop is costly. The string grows for each iteration, and each time you have to copy the entire previous string to create the new version.” Modern JavaScript engines are smart about this, and push/join tends to be slower. See stackoverflow.com/questions/7299010/….
@minitech: That may be true for some engines, but until we can say that it's true for all of them, the impact is horrible in enough of them.
It’s true for the current versions of all major web browsers. But anyway, it’s an implementation detail, so you should add a note about when your advice applies.
Does anyone including Guffa know if this is more effective than a ".map" function?
@Setsuna: The map function creates an array from the items, so that is the same principle. There may be some small performance difference, but they would react the same to a changed number of items.
1

map works well for this:

var students = [{name:"Jordan", age:"6"},{name:"Jake", age:"7"},{name:"Mark", age:"10"}];
var result = students.map(function(student) {
    return student.name + ',' + student.age;
});
alert(result.join('|'));

Comments

-1

Try this and see your console:

var string = '';
for (var s in students) {
    string += students[s].name + ', ' + students[s].age + ' | ';
}

console.log(string);

Fiddle: http://jsfiddle.net/80ss0u14/

I do not think it is costly to go on with such approach. It may be the most efficient way to iterate through the data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.