Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7161895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:38:40+00:00 2026-05-28T13:38:40+00:00

Compare: if (myVariable) { doSomething() } function doSomething () { // Work goes here

  • 0

Compare:

if (myVariable) {
    doSomething()
}

function doSomething ()
{
    // Work goes here
}

vs

doSomething();

function doSomething()
{
    if (myVariable) {
        // Work goes here
    }
}

ie My question is whether it’s faster to do the check outside of the function and avoid a context switch (I think that’s the right term) ) or just do it inside the function because it makes such a minor difference?

Cheers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:38:41+00:00Added an answer on May 28, 2026 at 1:38 pm

    It Just Doesn’t Matter (although the first method avoids some work so it should faster, but by an amount which is probably less than statistical noise).

    What really matters is which method best represents the logic. Rule of thumb is that every statement in a function should be on about the same level of abstraction. Is the conditional expression more or less abstract than the function call?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Compare these 2 queries. Is it faster to put the filter on the join
Is there possible to compare two images with different resolutions? I mean here some
Here is the compare operation I would like to do: // foobar is the
I'm attempting to compare two tables of equal length with a function, since I
Please compare these two codes. I can't understand why former one didn't work while
Compare to standard push/pull, what is the advantages of using the rebase command in
To compare the asymptotic order of the two functions, I calculated the limit of
I am trying to compare the performance of several Javascript libraries. Although measuring transaction
How do I compare 2 files, and produce a report showing the lines that
I need to compare dozens of fields in two objects (instances of the same

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.