Hi All, surprised to see many discussions about this, I'd say i learnt a lot:
1) return from loop 2) Array.some 3) return error instead of string 4) string is faster sometimes all are very valuable, after evaluating my use case, I choose 2 and 4, and reserve the rest for future use, very educational, thanks to all. Angelo On Mar 10, 5:14 am, Jimb Esser <[email protected]> wrote: > Though Errors definitely have their place, such as when an error > occurs, using an error for regular return codes during expected events > (assuming this function expects the look up to fail some times) is a > bit overkill, and horrible for performance. > > http://jsperf.com/a-string-is-not-an-error > > On Mar 9, 10:36 am, Roly Fentanes <[email protected]> wrote: > > > > > > > > > btwhttp://www.devthought.com/2011/12/22/a-string-is-not-an-error/ > > > On Friday, March 9, 2012 3:30:49 AM UTC-7, Angelo Chen wrote: > > > > Hi, > > > > Struggling to change coding practice, following has two samples, which > > > one is correct? or any other approaches available? > > > > exports.get_user = function (id, func) { > > > var found = false > > > for (var i = 0; i < users.length; i ++) { > > > if (users[i].id === id) { > > > found = true > > > func(null, users[i]) > > > break; > > > } > > > } > > > if (!found) > > > func("NOT_FOUND", null) > > > } > > > > exports.get_user = function (id, func) { > > > for (var i = 0; i < users.length; i ++) { > > > if (users[i].id === id) { > > > func(null, users[i]) > > > break; > > > } > > > if (i == users.length) > > > func("NOT_FOUND", null) > > > } > > > } -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
