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 4626182
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:22:55+00:00 2026-05-22T03:22:55+00:00

I am looking for a way, using static analysis of two JavaScript functions, to

  • 0

I am looking for a way, using static analysis of two JavaScript functions, to tell if they are the same. Let me define multiple definitions of “the same”.

Level 1: The functions are the same except for possible different whitespace, e.g. TABS, CR, LF and SPACES.

Level 2 The functions may have different whitespace like Level 1, but also may have different variable names.

Level 3 ???


For level one, I think I could just remove all (non-literal, which may be tough) whitespace from each string containing the two JS function definitions, and then compare the strings.

For level two, I think I would need to use something like SpiderMonkey’s parser to generate a two parse trees, and then write a comparer which walks the trees and allows variables to have different names.


[Edit] Williham, makes a good point below. I do mean identical. Now, I’m looking for some practical strategies, particularly with regards to using parse trees.

  • 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-22T03:22:56+00:00Added an answer on May 22, 2026 at 3:22 am

    Reedit:

    To expound on my suggestion for determining identical functions, the following flow can be suggested:

    Level 1: Remove any whitespace that is not part of a string literal; insert newlines after each {, ; and } and compare. If equal; the functions are identical, if not:

    Level 2: Move all variable declarations and assignments that don’t depend on the state of other variables defined in the same scope to the start of the scope they are declared in (or if not wanting to actually parse the JS; the start of the braces); and order them by line length; treating all variable names as being 4 characters long, and falling back to alphabetical ordering ignoring variable names in case of tied lengths. Reorder all collections in alphabetical order, and rename all variables vSNN, where v is literal, S is the number of nested braces and NN is the order in which the variable was encountered.

    Compare; if equal, the functions are identical, if not:

    Level 3: Replace all string literals with "sNN", where " and s are literal, and NN is the order in which the string was encountered. Compare; if equal, the functions are identical, if not:

    Level 4: Normalize the names of any functions known to be the same by using the name of the function with the highest priority according to alphabetical order (in the example below, any calls to p_strlen() would be replaced with c_strlen(). Repeat re-orderings as per level 1 if necessary. Compare; if equal, the functions are identical, if not; the functions are almost certainly not identical.


    Original answer:

    I think you’ll find that you mean “identical”, not “the same”.

    The difference, as you’ll find, is critical:

    Two functions are identical if, following some manner of normalization, (removing non-literal whitespace, renaming and reordering variables to a normalized order, replacing string literals with placeholders, …) they compare to literally equal.

    Two functions are the same if, when called for any given input value they give the same return value. Consider, in the general case, a programming language which has counted, zero-terminated strings (hybrid Pascal/C strings, if you will). A function p_strlen(str) might look at the character count of the string and return that. A function c_strlen(str) might count the number of characters in the string and return that.

    While these functions certainly won’t be identical, they will be the same: For any given (valid) input value they will give the same value.


    My point is:

    Determining wether two functions are identical (what you seem to want to achieve) is a (moderately) trivial problem, done as you describe.

    Determining wether two functions are truly the same (what you might actually want to achieve) is non-trivial; in fact, it’s downright Hard, probably related to the Halting Problem, and not something that can be done with static analysis.


    Edit: Of course, functions that are identical are also the same; but in a highly specific and rarely useful way for complete analysis.

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

Sidebar

Related Questions

I'm looking for a way using jquery/javascript to get a class name when I
I am simply looking for a way of using drag and drop without jquery
I am using codeigniter and looking a way to enable directly editting of doc
I am using the jquery-ui-dialog plugin I am looking for way to refresh the
I am looking for a more sophisticated way of using Linq to SQL. I
I looking for a way to programmatically start a VOIP call using the SIP
I'm looking for a way to format numbers using tokens. This needs to be
I'm currently using jQuery and am looking for a way to slide an image
I am looking for a way to search through every users profile using the
I'm looking for a way to map a simple insert stored procedure using NHibernate

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.