I am building the project structure and I was creating some modules, where I needed to import according to the route the user is on.
# my folder structure
modules
-- user
-- client
-- index
# my code
// get constructor
const const constructor = await getConstructor( 'user' ); // get the constructor
// index
export const getConstructor = async ( module ) => {
const constructor = await require(`./${module}`).create; // option 1
const constructor = await import(`./${module}`).then( constructor => constructor.create ); // option 2
return constructor;
}
// module - user
const create = ( data ) => {
// behavior
// ...
}
export {
create,
delete,
otherFunctions
}
My question is what is the best way, in terms of performance, to dynamically import the create
function, whether option 1 or 2 or even if there is another way.
Any suggestion?
import
some stuff on demand ?create
,delete
, other functions. And on that driver, I just wanted to get thecreate
function, without getting the rest. I'm thinking, in terms of performance.