Overview: So the issue is a limitation of WordPress as of version 4.8. WordPress is designed to load plugins and themes and all of its core every REST API request. Here is the reason for the slow response time.
Solution: The only current solution is an ajax call to a file in your plugin and loads only part of the WordPress core. The code below is direct file access while still being able to use WordPress functions with fast response time.
//Tell WordPress to only load the basics
define('SHORTINIT',1);
//get path of wp-load.php and load it
require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
// register global database
global $wpdb;
// return data selected from DB to user
Results: Response times are down to 100ms. That's a huge difference from 1069ms to 108ms.
Reference:
https://deliciousbrains.com/wordpress-rest-api-vs-custom-request-handlers/
Last notes:
The Wordpress REST API is very new, quite powerful and you should be using in most situations where response time is not an issue.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…