Another Coding Performance Question (Javascript) (1 Viewer)

BlueIshDan

☠
Local time
Today, 15:52
Joined
May 15, 2014
Messages
1,122
My goal is to figure out which method is better performance wise.

The two methods being compared are:
- IF ELSE
- Ternary operation = condition ? true : false

Code Examples:

IF ELSE
Code:
if (title != "filter by name")
{
    if(title.indexOf(searchvalue) > -1)
    {
        post.stop(true, true).show();
    } 
    else 
    {
        post.stop(true, true).hide();
    }
}

Ternary operation
Code:
(title != "filter by name") ? 
    ((title.indexOf(searchvalue) > -1) ? 
        post.stop(true, true).show() 
        : post.stop(true, true).hide()) 
    : null

Maybe a mix of the both could be considered best?

Code:
if (title != "filter by name")
{
    ((title.indexOf(searchvalue) > -1) ?
        post.stop(true, true).show()
        : post.stop(true, true).hide())
}

What do you think or, if you have the research skills, can prove is the most performance efficient way of executing this process? :D
 

MarkK

bit cruncher
Local time
Today, 11:52
Joined
Mar 17, 2004
Messages
8,179
You can do your own performance testing. This is how I would set it up in VBA. You can adapt it to javascript. Basically you do the operation an insanely large number of times, and time it so you get more than 0.0000001 seconds. Then you do the other operation the same number of times and time that. Then you know.

Code:
sub test1
   const MAX as long = 100000

   dim clock as single
   dim i as integer

[COLOR="Green"]   'test operation 1[/COLOR]
   clock = timer
   for i = 0 to MAX
[COLOR="Green"]      'do this operation MAX times[/COLOR]
   next
[COLOR="Green"]   'show how much time it took[/COLOR]
   debug.print "Op 1 ran for " & timer - clock & " seconds"

[COLOR="Green"]   'test operation 2[/COLOR]
   clock = timer
   for i = 0 to MAX
[COLOR="Green"]      'do this other operation MAX times[/COLOR]
   next
[COLOR="Green"]   'show how much time it took[/COLOR]
   debug.print "Op 2 ran for " & timer - clock & " seconds"
end sub
Hope this helps,
 

Bladerunner

Registered User.
Local time
Today, 11:52
Joined
Feb 11, 2013
Messages
1,799
The ternary operation will be faster but not by much. those extra If / Else add up. Not by much . I am talking very small numbers.



Blade
 

BlueIshDan

☠
Local time
Today, 15:52
Joined
May 15, 2014
Messages
1,122
Awesome thanks guys. I'll make a page to test this out! :)
I'll let you know what the results are!
 

Users who are viewing this thread

Top Bottom