For example, if I have to sort an array of data from a database to append it on the body of a web page. In terms of performance, should I sort it on the server-side and then send it to the client or should I send the data as it is and sort it on the client-side or it makes no difference?
This is a common problem that beginner, intermediate, and expert developers may struggle with. It’s really an architectural question that is best answered with as much context as possible.
What is your API running on? If you’re hosting in the cloud (AWS, Azure, etc.), are you trying to minimize hosting costs by minimizing resource usage? If so, pushing that work onto clients may help at scale, but is probably negligible for small applications.
What about your clients? Are you only supporting desktop machines in an enterprise environment where there’s a high probability that you can generalize their specifications? Or are IoT devices with minimal computing resources contacting your API? In the latter case the client device may not have the memory or computational power to sort the data while still meeting the rest of its duties. What about mobile devices that may be 10-20 years old?
Is this fixed behavior on your API or can clients request server-side sorting? What sort of flexibility do you want your API to provide and how feasible is it to implement switches and parameters for every facet? At what point are you doing too much, obscuring the purpose of the API with a plethora of buttons and dials to configure or tune.
In my personal experience, I like ORMs like Sequelize that can expose a lot of flexibility on your API with little work, but these tools can come at a computational cost and potential (but usually infrequent) security issues. They’re very general implementations that aren’t always perfect for your use case.
Answered By – Zulfe
Answer Checked By – Clifford M. (BugsFixing Volunteer)