-1

I am currently working on a web page development. I have used downloaded one external javascript file from CanvasJs and I am using it locally to add some dynamic functionality to a graph in my page. I am using only a small functionality from the file. How do i remove the dead line that I don't use? I don't know which lines are not being used!

Note: I am using the script file to draw a spline chart, but the script file contains code for many more graphs and functionalities. How do I remove those redundant functionalities. The script file is too big with all the functions that I don't use. I wish to reduce the file size by removing the redundant line of codes.

  • 1
    Possible duplicate of [How to Remove Unused javascript From Website](https://stackoverflow.com/questions/41478936/how-to-remove-unused-javascript-from-website) – Pac0 Nov 27 '18 at 08:01

1 Answers1

0

If you are certain that you want to remove all but the code that your application calls:

  1. Find all of the functions that your code calls in the library.
  2. Copy each of these functions into a new file.
  3. In each of these functions, find all functions and any global variables that they reference and return to point 2.
  4. When you have all of the referenced functions and variables in a separate file, try your code out on it in as many different ways as you can think of to make sure that you did not miss anything out.

Do this on the un-minified version of the library and then minify the resulting code, so that it is readable for you.

Another way of stepping through the code is to use the debugger built in to your browser, by setting a breakpoint on your own code and stepping through it, then finding that code in the library file.

If you think that you might need more functionality from the library however, weigh the cost over the wire against the effort of extracting only the required code. It might be best to use the full library or to find another more specialised library if possible.

Billy Brown
  • 2,272
  • 23
  • 25
  • I don't know how to find the codes that are being called? Is there any way to find out the functions that are called? – Thomas Brolin Nov 28 '18 at 05:48
  • Your own code must call the library code somewhere. If you have a non-minimised version of the library, Cmd-F/Ctrl-F `functionName` to find the function that your code calls. From there, repeat this. Another option is to use the debugger in the browser, as that will show you exactly what code your own code calls. Pac0's comment on your question might also be an option (tools for finding unused code). – Billy Brown Nov 28 '18 at 08:31