-2
\$\begingroup\$
;(function update_tree(jsonml) { for(i = 1; i < jsonml.length; i++) { if(jsonml[i][0] === 'para' && typeof jsonml[i][1] === "string" && jsonml[i][1].match(/"/g)) { // some code } if(jsonml[i][0] === 'hr') { // some code } if(jsonml[i][0] === 'h2') { // some code } } })(tree) 

What the code does is to take a JsonML tree like this:

[ 'markdown', [ 'para', 'This is a ', [ 'em', 'test' ] ], [ 'hr' ], [ 'para', 'another test' ], [ 'para', '"and te fen eternte t"' ], [ 'hr' ], [ 'para', 'ert wte wet wntwet wte w' ] ] 

And modify the data in it. For instance, the fist if statement checks for paras with double quotes. I'll have many of these if statements, say, to check for h3, h4 tags or other HTML entities.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Please state only the code's purpose in the title. \$\endgroup\$ Commented Mar 2, 2015 at 17:02
  • \$\begingroup\$ Please explain what this code does. It seems to be broken due to formatting. Also, avoid placeholders like // do stuff, as that makes a vague question even more hypothetical. \$\endgroup\$ Commented Mar 2, 2015 at 17:16
  • \$\begingroup\$ I modified the question. How about now? \$\endgroup\$ Commented Mar 2, 2015 at 17:21
  • 1
    \$\begingroup\$ The context is better. The code is worse than before. \$\endgroup\$ Commented Mar 2, 2015 at 17:30

1 Answer 1

1
\$\begingroup\$

else if would be more efficient because it wouldn't continue to evaluate comparisons that you already know won't match.

It would also be more efficient to cache some values here so you aren't revaluating the same expression over and over.

A switch statement could work fine. I would tend to use a switch or an object table lookup if you had lots of different tag names you were looking for. At just qty 3, I'd probably stick to the else if.

Here's an implementation with else if and some expression caching:

;(function update_tree(jsonml) { var item, tag; for (i = 1; i < jsonml.length; i++) { item = jsonml[i]; tag = item[0]; if (tag === 'para') { if (typeof item[1] === "string" && item[1].match(/"/g)) { console.log(item); item[1] = item[1].replace(/"/g, "“"); } } else if (tag === 'hr') { var p = jsonml[i + 1]; item.splice(0, 1, 'para', '* * *'); p.splice(1, 0, {'class': 'noind'}); } else if (tag === 'h2') { // do stuff } } })(tree); 

If there were many, many tags, I would use a table lookup:

(function(tree) { var tagOps = { "para": function(item) { // code here }, "hr": function(item) { // code here }, "h2": function(item) { // code here } }; var item, fn; for (i = 1; i < tree.length; i++) { item = tree[i]; fn = tagOps(item[0]); if (fn) { fn(item); } } })(tree); 
\$\endgroup\$
7
  • \$\begingroup\$ There'll be many in the future. So if I use a switch statement, they'll re-evaluate each time? \$\endgroup\$ Commented Mar 2, 2015 at 17:17
  • \$\begingroup\$ Please see my updated question. \$\endgroup\$ Commented Mar 2, 2015 at 17:21
  • \$\begingroup\$ If there were many tags, I'd use a function table lookup in an object indexed by the tag. \$\endgroup\$ Commented Mar 2, 2015 at 17:27
  • \$\begingroup\$ @janoChen - code added to show a table lookup strategy. \$\endgroup\$ Commented Mar 2, 2015 at 17:37
  • 1
    \$\begingroup\$ @janoChen - As I said in my answer, a switch() statement would work fine. I feel like I never know how efficient a large switch would be in a given JS implementation (whether it's just doing a series of if/else comparisons or something more efficient than that) so I choose to implement a table lookup instead. I feel like the table lookup encapsulates the code for each tag cleaner and there's no way to accidentally forget a break; statement in a switch, but that's just my own personal preference, not a general guideline. \$\endgroup\$ Commented Mar 2, 2015 at 17:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.