Detect string duplication (Performance Optimization)

Feature Request

I have an idea in my head. Not sure if it’s already implemented.

In JavaScript modules, Babel could detect same strings and create one small variable for that.
Then It would replace those duplicates with that variable. It could prevent the JS engine from creating strings every single time.

It might even decrease the file size. Like Google does it with CSS class names.

Example

// Original
const doBlah = _ => {
    const arr = [
        { name: "John", age: 19 },
        { name: "Sophia", age: 12 },
        { name: "Max", age: 45 }
    ];

    console.log(arr[0]["name"]);
    console.log(arr[1]["name"]);
    console.log(arr[2]["name"]);

    console.log(arr[0]["age"]);
    console.log(arr[1]["age"]);
    console.log(arr[2]["age"]);

    arr.push({ name: "Kate", age: 23 });

    console.log(arr[3]["name"]);
    console.log(arr[3]["age"]);
};
// Babel (Without converting back to es5)
const doBlah = _ => {
    const arr = [
        { name: "John", age: 19 },
        { name: "Sophia", age: 12 },
        { name: "Max", age: 45 }
    ];

   // Extra variables
   var a = "name", b = "age";

    // Replacing strings with variables
    console.log(arr[0][a);
    console.log(arr[1][a]);
    console.log(arr[2][a]);

     //  Replacing strings with variables 
    console.log(arr[0][b]);
    console.log(arr[1][b]);
    console.log(arr[2][b]);

    arr.push({ name: "Kate", age: 23 });

     //  Replacing strings with variables 
    console.log(arr[3][a]);
    console.log(arr[3][b]);
};

1 possible answer(s) on “Detect string duplication (Performance Optimization)