用 slice 解析 JSON 时性能有问题吗?
When parsing JSON with slice is performance an issue?
我是 JSON 领域的新手,过去一周我就仅显示特定数量和使用按钮点击显示 X 计数的主题进行了大量研究在我最初加载 12 之后。我学会了如何使用 PHP、 调用 API,使用带有 PHP 的 foreach 语句来输出 JSON 内容但是当我使用按钮显示下一个数量时,如果性能受到影响,我并没有找到明确的答案或解释。
我调用的 JSON 有超过 100 个对象,每个对象最多有 20 个值(阅读 here) and it's updated every hour with PHP. I thought it would be good performance to render the first 12 with PHP after reading how to limit the statement 后我对术语的理解:
$showList = 12;
$countRecords = 0;
foreach($products as $product) {
if ($countRecords < $showList) {
// more code
}
++$countRecords;
}
并阅读了一些建议在服务器端执行此操作的问题。
在参考了几个关于如何创建按钮点击加载下一个 12 计数的问题后,我担心使用 .slice()
会影响性能,因为它看起来每次都遍历整个文件然后切片内容:
我确实看到了另一种方法,其中添加了隐藏的 class 并且按钮删除了 class 但这不会影响性能:
AJAX的另一种方法:
- Only return first 20 results of JSON data passed by $.ajax?
我确实考虑过修改计数器:
- jQuery - Increase the value of a counter when a button is clicked
如果我有一个大的 JSON 文件,单击 Ajax 按钮不会延迟 return 或影响性能的适当执行是什么还是 .slice()
没有效果?
据我所知,jQuery 中的任何解决方案(或任何客户端)在您限制它之前仍然 return 整个 JSON,并且由于您这样做的目的是性能,那是没用的。
您需要限制 returned JSON 服务器端。您仍然可以使用 jQuery 作为 ajax,但不是直接调用您的 JSON,而是创建一个限制响应的 PHP 文件。
jQuery
// set initial variables
var limit = 10;
var offset = 0;
// get data on button click
$.('#load-more').on('click', function(){
getData();
});
function getData() {
$.ajax({
url:"ajax.php",
type: "POST",
data: { limit: limit, offset: offset },
success: function(json) {
// increase offset for next call
offset++;
// do something with your JSON
},
});
}
ajax.php
// get your POST variables
if( isset($_POST['limit']) ) $limit = (int)$_POST['limit'];
if( isset($_POST['offset']) ) $offset = (int)$_POST['offset'];
// get your JSON
if ( isset($limit) && isset($offset) ) {
$from = ($offset + 1) * $limit;
$to = $from + $limit;
$json = // get JSON objects $from to $to
echo $json;
}
您如何在 ajax.php 中获得正确的 JSON 结果取决于您如何 and/or 存储您的 JSON。
如果您正在进行 API 调用,请查看 API 具有哪些 limit/offset 参数。根据 API,您可以使用 $offset
& $limit
或 $from
& $to
。如果你正在这样做,你可以直接从你的 ajax 电话中进行。
如果您将 JSON 存储在数据库中,您可以通过 MYSQL 查询获得结果。
如果您只有一个 JSON string/file,您可以像第一个 PHP 示例一样循环遍历它。像这样:
// get your POST variables
if( isset($_POST['limit']) ) $limit = (int)$_POST['limit'];
if( isset($_POST['offset']) ) $offset = (int)$_POST['offset'];
// get your JSON
if ( isset($limit) && isset($offset) ) {
// limit & offset
$from = ($offset + 1) * $limit;
$to = $from + $limit;
// get existing json
$products = json_decode($existing_json, true);
$countRecords = 0;
$products_to_return = array();
foreach($products as $product) {
// skip to next product if we are before $from
if ($countRecords < $from) continue;
// end the loop if we are after $to
if ($countRecords > $to) break;
$products_to_return[] = $product;
++$countRecords;
}
// encode your new limited json
$json = json_encode($products_to_return);
echo $json;
}
注意—none 这段代码已经过测试,很可能充满错误(晚了!)但它让您知道该怎么做
我是 JSON 领域的新手,过去一周我就仅显示特定数量和使用按钮点击显示 X 计数的主题进行了大量研究在我最初加载 12 之后。我学会了如何使用 PHP、
我调用的 JSON 有超过 100 个对象,每个对象最多有 20 个值(阅读 here) and it's updated every hour with PHP. I thought it would be good performance to render the first 12 with PHP after reading how to limit the statement 后我对术语的理解:
$showList = 12;
$countRecords = 0;
foreach($products as $product) {
if ($countRecords < $showList) {
// more code
}
++$countRecords;
}
并阅读了一些建议在服务器端执行此操作的问题。
在参考了几个关于如何创建按钮点击加载下一个 12 计数的问题后,我担心使用 .slice()
会影响性能,因为它看起来每次都遍历整个文件然后切片内容:
我确实看到了另一种方法,其中添加了隐藏的 class 并且按钮删除了 class 但这不会影响性能:
AJAX的另一种方法:
- Only return first 20 results of JSON data passed by $.ajax?
我确实考虑过修改计数器:
- jQuery - Increase the value of a counter when a button is clicked
如果我有一个大的 JSON 文件,单击 Ajax 按钮不会延迟 return 或影响性能的适当执行是什么还是 .slice()
没有效果?
据我所知,jQuery 中的任何解决方案(或任何客户端)在您限制它之前仍然 return 整个 JSON,并且由于您这样做的目的是性能,那是没用的。
您需要限制 returned JSON 服务器端。您仍然可以使用 jQuery 作为 ajax,但不是直接调用您的 JSON,而是创建一个限制响应的 PHP 文件。
jQuery
// set initial variables
var limit = 10;
var offset = 0;
// get data on button click
$.('#load-more').on('click', function(){
getData();
});
function getData() {
$.ajax({
url:"ajax.php",
type: "POST",
data: { limit: limit, offset: offset },
success: function(json) {
// increase offset for next call
offset++;
// do something with your JSON
},
});
}
ajax.php
// get your POST variables
if( isset($_POST['limit']) ) $limit = (int)$_POST['limit'];
if( isset($_POST['offset']) ) $offset = (int)$_POST['offset'];
// get your JSON
if ( isset($limit) && isset($offset) ) {
$from = ($offset + 1) * $limit;
$to = $from + $limit;
$json = // get JSON objects $from to $to
echo $json;
}
您如何在 ajax.php 中获得正确的 JSON 结果取决于您如何 and/or 存储您的 JSON。
如果您正在进行 API 调用,请查看 API 具有哪些 limit/offset 参数。根据 API,您可以使用
$offset
&$limit
或$from
&$to
。如果你正在这样做,你可以直接从你的 ajax 电话中进行。如果您将 JSON 存储在数据库中,您可以通过 MYSQL 查询获得结果。
如果您只有一个 JSON string/file,您可以像第一个 PHP 示例一样循环遍历它。像这样:
// get your POST variables
if( isset($_POST['limit']) ) $limit = (int)$_POST['limit'];
if( isset($_POST['offset']) ) $offset = (int)$_POST['offset'];
// get your JSON
if ( isset($limit) && isset($offset) ) {
// limit & offset
$from = ($offset + 1) * $limit;
$to = $from + $limit;
// get existing json
$products = json_decode($existing_json, true);
$countRecords = 0;
$products_to_return = array();
foreach($products as $product) {
// skip to next product if we are before $from
if ($countRecords < $from) continue;
// end the loop if we are after $to
if ($countRecords > $to) break;
$products_to_return[] = $product;
++$countRecords;
}
// encode your new limited json
$json = json_encode($products_to_return);
echo $json;
}
注意—none 这段代码已经过测试,很可能充满错误(晚了!)但它让您知道该怎么做