I want to import data from a CSV file to spring controller, as far as I know, there are 2 ways:
- parsing the data on the client side with a library like papaparse.v.v then sending it to the controller
- uploading the file and then parsing it on the server side which is the best way in the above two ways, thanks a lot
CodePudding user response:
Each way also have pros and cons.
If you have only few user/client then parsing at server maybe more suitable.
Otherwise, parsing at clients should be consider because it reduce the workload at server. It should be consider with priority if your application have thousand or million request each day.
CodePudding user response:
First of all, unless the CSV file is big or you are doing this operation a frequently, the performance difference between the two approaches is likely to be irrelevant to the user ... and to you.
But lets compare the two ways of doing this:
Client side CSV parsing: The client side CSV parser will most likely be fast ... and you are using the user's CPU to do it. But when if / when you send the data to the server, you are going to have to reserialize it all (e.g. as JSON). And the serialized form will most likely need to be deserialized on the server side.
So overall you will save little if any time on the server side, and add the cost of parsing and serializing on the user's machine.
However, if you can do some (significant) data reduction on the client side before sending the data to the server, you may end up with a saving. It will depend on how much you can reduce the data, and client <-> server network data rates.
Server side CSV parsing. This avoids the additional serialization / deserialization overheads. But there is no opportunity for any data reduction.
In short ... it depends on the circumstances.
But my advice is: "Avoid Premature Optimization". Don't try guess which approach will be more efficient. Implement the simpler version, and only optimize (your working system!) if there is a demonstrated need to optimize.