Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what scenarios is it better to use json_encode() than to use serialize()?

Tags:

php

That's about all I need to ask. Checked the PHP manual and saw a user post saying that serialize is 45-90% slower than json_encode (he ran some benchmarks). But how "slow" is slow? I can find a lot of "versus" stuff sprawling around but none of which a beginner like me can relate to.

I just wrote a script that encoded an array in json and another one to decode it. I did same with serialize. Obviuously that won't tell me any significant differences between them.

like image 532
yretuta Avatar asked Jan 28 '26 14:01

yretuta


1 Answers

Do that 10,000 times (each) to (hopefully) get a measurable idea of differences in both memory usage and CPU time.

Mostly the difference won't be significant in terms of performance. Using JSON is useful in two particular circumstances: for returning to a Web browser and for interoperability with other applications (via Web services and other means), particularly those on non-PHP platforms.

like image 142
cletus Avatar answered Jan 31 '26 03:01

cletus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!