diff --git a/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php b/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php
index e0b9b92..e981eeb 100644
--- a/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php
+++ b/core/modules/menu/lib/Drupal/menu/Tests/MenuTest.php
@@ -251,7 +251,7 @@ function doMenuTests($menu_name = 'tools') {
     $this->assertMenuLink($item3['mlid'], array('depth' => 4, 'has_children' => 0, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => $item2['mlid'], 'p4' => $item3['mlid'], 'p5' => 0));
 
     // Add 102 menu links with increasing weights, then make sure the last-added
-    // item's weight doesn't get changed because of the old hardcoded delta=50
+    // item's weight doesn't change because of the old hardcoded delta = 50.
     $items = array();
     for ($i = -50; $i <= 51; $i++) {
       $items[$i] = $this->addMenuLink(0, 'node/' . $node1->nid, $menu_name, TRUE, strval($i));
diff --git a/core/modules/menu/menu.admin.inc b/core/modules/menu/menu.admin.inc
index 3b67b97..dfe3780 100644
--- a/core/modules/menu/menu.admin.inc
+++ b/core/modules/menu/menu.admin.inc
@@ -109,7 +109,7 @@ function menu_overview_form($form, &$form_state, $menu) {
  * @param $tree
  *   The menu_tree retrieved by menu_tree_data.
  * @param $delta
- *   The default number of menu items used in the menu weight selector is 50.
+ *   The number of menu items used in the menu weight selector; defaults to 50.
  */
 function _menu_overview_tree_form($tree, $delta = 50) {
   $form = &drupal_static(__FUNCTION__, array('#tree' => TRUE));
@@ -388,14 +388,10 @@ function menu_edit_item($form, &$form_state, $type, $item, $menu) {
     '#attributes' => array('class' => array('menu-title-select')),
   );
 
-  // Get number of items in menu so the weight selector is sized appropriately.
-  $sql = "SELECT COUNT(*) FROM {menu_links} WHERE menu_name = :menu";
-  $result = db_query($sql, array(':menu' => $item['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
-  foreach ($result as $row) {
-    foreach ($row as $menu_item_count) {
-      $delta = $menu_item_count;
-    }
-  }
+  // Get the max and min weights of the items in the menu so the weight selector
+  // is sized appropriately.
+  $weight_info = db_query("SELECT MAX(weight) AS max_weight, MIN(weight) as min_weight FROM {menu_links} WHERE menu_name = :menu", array(':menu' => $item['menu_name']))->fetchObject();
+  $delta = max(abs($weight_info->min_weight), abs($weight_info->max_weight)) + 1;
   if ($delta < 50) {
     // Old hardcoded value.
     $delta = 50;
diff --git a/core/modules/menu/menu.module b/core/modules/menu/menu.module
index 73e4380..d65dc31 100644
--- a/core/modules/menu/menu.module
+++ b/core/modules/menu/menu.module
@@ -709,14 +709,10 @@ function menu_form_node_form_alter(&$form, $form_state) {
     '#attributes' => array('class' => array('menu-parent-select')),
   );
 
-  // Get number of items in menu so the weight selector is sized appropriately.
-  $sql = "SELECT COUNT(*) FROM {menu_links} WHERE menu_name = :menu";
-  $result = db_query($sql, array(':menu' => $link['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
-  foreach ($result as $row) {
-    foreach ($row as $menu_items) {
-      $delta = $menu_items;
-    }
-  }
+  // Get the max and min weights of the items in the menu so the weight selector
+  // is sized appropriately.
+  $weight_info = db_query("SELECT MAX(weight) AS max_weight, MIN(weight) as min_weight FROM {menu_links} WHERE menu_name = :menu", array(':menu' => $link['menu_name']))->fetchObject();
+  $delta = max(abs($weight_info->min_weight), abs($weight_info->max_weight)) + 1;
   if ($delta < 50) {
     // Old hardcoded value
     $delta = 50;
