I work on an open source project - object document mapper aka. ODM
in javascript
and am facing a design decision I struggle with (rather simple
but not easy
) .
In the ODM
you can define a Model
object which describes your data document in a database. When creating a new Model
instance object you can pass in it's data values via an argument.
Pseudocode:
var data = {
username: 'james'
email: 'james@email.com',
country: {
code: 'US',
city: ''
}
};
// In case the data object would not be cloned, it would be mutated by the ODM
var user = new UserModel(data);
Now, the decision I'm facing is whether to automatically clone data
object in the Model before it's processed by the application (ODM). I incline to a choice of not cloning the input data. I feel like that in the javascript
community it's quite popular to clone data usually more than it's necessary because it can make things easy
(not simple
) for end-users. From my research I've found out that some similar and popular projects made a decision to clone the data, yet I'm not sure that's the right choice considering a Model can have large & complex data schema and while it does not make any difference in case of tens of Model
instance objects, I think it could be an issue while working with eg.: hundreds of Model
objects.
I'd love to hear your reasoning on the topic and what would be your choise?
In case you already see another answer, please, don't hesitate to write down your thoughts!
Thank you!