Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have an array of this fashion:

var arr = ["akira",9248,"padilla",100];

I'm using filter to take the names and the points:

var names = arr.filter(function(e,i){return i%2 == 0});
var points = arr.filter(function(e,i){return i%2 !== 0});

Then I do this to obtain turn it into object:

console.log(JSON.stringify(_.object(names,points)));

What's best in terms of performance/JS quality?

share|improve this question
    
I'd be surprised if you could beat the plain for loop: jsfiddle.net/jfriend00/ws705w5c – jfriend00 Oct 9 at 23:17

3 Answers 3

up vote 4 down vote accepted

Could you do it this way which would mean you're only traversing the array once?

var obj = {};

for (var i=0; i < arr.length - 1; i = i + 2) {
    obj[arr[i]] = arr[i+1];
}         
share|improve this answer

I was wondering if I could do the same with reduce... and I did:

var arr = ["akira",9248,"padilla",100];

var obj = arr.reduce((carry, item, i, array) => {
  if(!(i%2)) carry[item] = array[i+1];
  return carry;
}, {});

document.write(JSON.stringify(obj));

share|improve this answer
    
You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and how it improves upon the original) so that the author can learn from your thought process. – SuperBiasedMan Oct 13 at 11:48
    
@SuperBiasedMan *"Make sure your answer provides that – or a viable alternative. The answer can be “don’t do that”, but it should also include “try this instead”" codereview.stackexchange.com/help/how-to-answer. Besides, the other guy pretty much already answered a more optimized version, and reduce is pretty much self-explanatory if you read what it does. – Joseph the Dreamer Oct 13 at 12:03
    
But you haven't explained why reduce is better or what's bad about the original code. That's what I think you should add. reduce may not be self explanatory to the OP, that might be why they didn't use it. – SuperBiasedMan Oct 13 at 12:06

My suggestion is:

var arr = ['akira', 9248, 'padilla', 100];
var obj = {};
var len = arr.length;
for (; len;) {
  obj[arr[--len]] = arr[--len];
}
alert(JSON.stringify(obj));

Simple for much faster then filter.
https://jsperf.com/javascript-array-filter-versus-loop

Iterating through an array backwards faster then forwards.
http://jsperf.com/array-iteration-direction

share|improve this answer

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

1  
Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and how it improves upon the original) so that the author can learn from your thought process. – SuperBiasedMan Oct 13 at 11:39
    
Thank you, will do. – Valentin Oct 13 at 11:44
1  
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. – Mast Oct 13 at 11:49
1  
The question is "What's best in terms of performance/JS quality?" and my answer is exactly about performance. – Valentin Oct 13 at 11:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.