PHP ::: Speed Test ::: $_SESSION vs. $variable

前端 未结 3 1607
臣服心动
臣服心动 2020-12-21 16:16

I have a form with a fairly large amount of input that will also be high traffic. I declare $_SESSION vars so that on validation fails data that passes doesnt have to be rew

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-21 17:01

    I haven't done any benchmarks on this, but I strongly suspect that $_SESSION[] will always be a little slower than normal variables for writes, because it's backed by the filesystem or database. Even if all $_SESSION[] storage is cached, there will still be more CPU and memory activity involved in using it than in using variables that are only held locally. For reads, there will still likely be a small difference, but it'll be negligible compared to other factors.

    So the comment above is right on the money. You have other areas of optimization that are more important than this one.

    If you're looking to improve performance, consider duplicating some of your input validation in JavaScript. (Don't REPLACE server-side validation, as JS is not universal; just consider ways to avoid requiring it if possible.)

提交回复
热议问题