What are some ways to enhance Javascript performance using Closure Compiler while preserving function names?

Seeking a solution for using specific functions from a Javascript file (lib.js) in a webpage without loading the entire file.

I am looking to achieve this through command line scripting, but have not yet found a method that works as desired.

The content of lib.js:


        function dog() {
            return 'Fido';
        }

        function famous_human() {
            return 'Winston';
        }

        function human() {
            return famous_human();
        }
    

Code snippet calling functions from lib.js:


        alert(human());
    

Desired output in lib-compiled.js:


        function a() {return 'Winston';}
        function human() {return a();}
    
  • Function dog has been omitted due to non-usage.
  • Function famous_human has been optimized.
  • Function human retains its original name for external calling purposes.
  • No code included from the snippet code-calling-functions-in-lib.js

Seeking help on how to achieve this result with a simple approach. Is there an easy solution?

Answer №1

If you want to export a human:

function human() {
    return famous_human();
}

window['human'] = human;

For more details on exporting functions, visit this link

It's important to note that exporting symbols can prevent dead-code elimination, so it's recommended to keep project-specific exports in a separate file rather than including them in your library.

For example

Library Source - liba.js

function dog() {
  return 'Fido';
}

function famous_human() {
  return 'Winston';
}

function human() {
  return famous_human();
}

Project Specific Exports - project_exports.js

window['human'] = human;

Compilation command

java -jar compiler.jar \
  --compilation_level ADVANCED_OPTIMIZATIONS \
  --js liba.js \
  --js project_exports.js \
  --js project_source.js \
  --js_output_file project_compiled.js

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Developing dynamic 3D cubes

In my latest project, I am attempting to construct a unique structure composed of 3D cubes arranged in a stacked formation. Each of the individual cubes within this structure is designed to be interactive for users. When a user hovers over a cube, it trigg ...

Render a component in certain routes based on specific conditions in React

This is my Index.js ReactDOM.render( <React.StrictMode> <Provider store={store}> <Router> <App className="app-main" /> </Router> </Provider> </R ...

Adjusting the height of content in Angular Material 2 md-tab

I've recently started working with angular material and I'm facing an issue while trying to implement md-tab into my application. The problem is that I can't seem to style my tab content to take up the remaining height of the screen. Could s ...

Conceal Pictures within Sources Section

On my webpage, I have included images with information about my project. To prevent users from downloading the images, I disabled the download option. However, users are still able to access all the images in the Sources tab by inspecting the page. Can a ...

Expanding Headers with JavaScript

Looking to add a Stretchy Header Functionality similar to the one shown in this GIF: Currently, on iPhones WebView, my approach involves calling a Scope Function On Scroll (especially focusing on Rubberband Scrolling) and adjusting the Image Height with C ...

The state in useState is failing to update correctly following selections made within the dropdown menus

I am currently facing an issue with my dropdown disabling function, which is not enabling the dropdown properly. I suspect that this is due to asynchronous problems stemming from the use of useState. const [homeSelect, setHomeSelect] = useState('Home& ...

Create an array of dynamically calculated properties from the Vuex state array, which can then be utilized in the v-model

In my Vue 3 setup, I have a Vuex store with an array in the state: const store = createStore({ state: { questions: [ { text: 'A', value: false }, { text: 'B', value: false }, { text: 'C', value: true }, ...

Bootstrap popover fails to function without any visible errors

Currently in the process of developing a website with the latest version of Bootstrap, I'm looking to implement a feature where a popover appears upon clicking a specific button. After including the necessary jQuery and popper.js files in my project, ...

most efficient method of sharing information between various angular controllers

I am looking for a way to share form data among multiple controllers before submitting it. Currently, I am using module.value() to store the data as a global variable. var serviceApp = angular.module('sampleservice', [ ]); serviceApp.valu ...

Effortlessly upload multiple dynamic files using Nest JS

Attempting to upload files with dynamic keys, however nest.js requires key names. Attempted solution: @UseInterceptors(FilesInterceptor('files')) async uploadFile(@Query() minioDto: MinioDto, @UploadedFiles() files: Array<BufferedFi ...

Splitting Code in React Components using Webpack within a Ruby on Rails Application

I'm currently integrating ReactJS components into my Rails application using the Webpack gem. However, I am facing an issue where the React components are only being loaded in specific areas within the layout of the Rails application. This results in ...

There are no specified operations outlined in the Node.js Express documentation

swagger ui https://i.stack.imgur.com/UIavC.png I've been struggling to resolve this issue where the /swagger endpoint seems to only partially read the swagger.json file. Despite configuring everything correctly, no errors are appearing. It simply dis ...

Retrieve the value of a dropdown menu by referencing its name in JQuery

My webpage contains the following dropdowns: <select id="select_status_id44" name="select_status_id[44]" class="inputbox" id = "status_id_44"> <option value="1">Pending</option> <option value="2" selected="selected">Confirmed& ...

Guide to preventing dragging when resizing a card in React with the @dnd-kit/core library

My React component utilizes the @dnd-kit/core library for dragging and the re-resizable library for resizing. The card in this component can be both dragged and resized, but I am encountering an issue where resizing also triggers dragging. I want to preven ...

Check that EACH radio button has been either selected or left unselected when the button is clicked

I'm still learning Javascript and currently working on a function triggered by a button click. My goal is to display an alert if NONE of the radio buttons have been selected, regardless of whether they are "Yes" or "No." If ALL radio buttons have been ...

What is the best way to access a variable in one file and then utilize it in multiple other files?

My question may seem straightforward, but I'm having trouble finding the right solution. Within my index.html file, I have the following content: <html> <head> <script src="scalar.js"></script> </head> <body> < ...

Combatting repetitive code through the use of Redux toolkit and actions

My code is currently long and repetitive. I realize that using helper functions would help me cut it down and make it more maintainable and readable. As a React beginner, I have a question: Should I implement most of this logic with helper functions in a s ...

NgZone is no longer functioning properly

Seemingly out of the blue, my NgZone functionality has ceased to work. I'm currently in the process of developing an application using Ionic, Angular, and Firebase. An error is being thrown: Unhandled Promise rejection: Missing Command Error ; Zon ...

execute the function once the filereader has completed reading the files

submitTCtoDB(updateTagForm:any){ for(let i=0;i<this.selectedFileList.length;i++){ let file=this.selectedFileList[i]; this.readFile(file, function(selectedFileList) { this.submitTC(updateTagForm,selectedFileList); }); } } } ...

Generate visual at reduced quality (three.js)

Is there a way to reduce the resolution of my rendered canvas similar to how it can be done with the Blender camera resolution? I came across a suggestion to use renderer.setDevicePixelRatio(window.devicePixelRatio) However, when I tried this, the objec ...