REST API vs Direct Access Code

时间:2015-12-14 18:06:53

标签: javascript php html static

With the rise popularity of REST API and Static Website Generators these days, I'm a bit confused regarding the performance of using REST API vs accessing the code directly.

If I have a piece of code that I want to reuse, should I turn it into a REST API or just leave as it is for faster performance?

The code in question is written in PHP and I plan to use Jekyll or another Static Website Generator and JavaScript to communicate with the code.

For example, to access the code directly, I would use GET http://example.com/file.php?function and return the pain result. To access through REST API, GET http://example.com/function and return the JSON result.

Based on the speed difference, which loads faster in a web browser, REST API or direct? Or are they the same speed?

Thanks.

1 个答案:

答案 0 :(得分:0)

这取决于您将如何重用代码。

如果您需要通过其他Web应用程序重用该函数,则可以选择REST API。否则,直接代码访问会更快。

REST API将隔离您的功能并使其可重用,但它也会增加不必要的完整性。

您可能考虑的另一件事是您要传输的数据量......建议使用REST来传输小块数据。