Измерение скорости выполнения JavaScript

console.time('checkFor1');
    for (var i = 1; i >= 100000; i++) {
        let manipulation = webix.copy(this.app.getService("manipulationAddPanel").getItem());
        manipulation.$parent = parent.id;
        manipulation.manipulation_id = manipulation.id;
        delete manipulation.id;	
    }
console.timeEnd('checkFor1');
	
console.time('checkFor2');
    for (var i = 1; i >= 100000; i++) {
        let manipulation = this.app.getService("manipulationAddPanel").getItem();
        let item = {$parent: parent.id, manipulation_id: manipulation.id, value: manipulation.value, active: manipulation.active};
    }
console.timeEnd('checkFor2');
						

Почти в 2 раза быстрее создать объект из существующего чем копировать его и потом изменять




Опции форматирования даты

  • %d — the day as a number with a leading zero ( 01 to 31 );
  • %j — the day as a number without a leading zero ( 1 to 31 );
  • %D — the day as an abbreviation ( Sun to Sat );
  • %l — the day as a full name ( Sunday to Saturday );
  • %m — the month as a number with a leading zero ( 01 to 12 );
  • %n — the month as a number without a leading zero ( 1 to 12);
  • %M — the month as an abbreviation ( Jan to Dec );
  • %F — the month as a full name ( January to December );
  • %y — the year as a two-digit number ( 00 to 99 );
  • %Y — the year as a four-digit number ( 1900–9999 );
  • %h — the hour based on the 12-hour clock with a leading zero ( 00 to 11 );
  • %g — the hour based on the 12-hour clock ( 0 to 11 );
  • %H — the hour based on the 24-hour clock with a leading zero ( 00 to 23 );
  • %G — the hour based on the 24-hour clock ( 0 to 23 );
  • %i — the minute as a number with a leading zero ( 00 to 59 );
  • %s — the second as a number with a leading zero ( 00 to 59 );
  • %S — the milliseconds as a number with a leading zero (000 to 999);
  • %a — displays am (for times from midnight until noon) and pm (for times from noon until midnight);
  • %A — displays AM (for times from midnight until noon) and PM (for times from noon until midnight);
  • %c — displays date and time in the ISO 8601 date format (e.g. 2015-10-04T05:04:09)


Проверить записи на изменение значений

SELECT model.case_part_id, model.case_part_question_id, answers.question_value,
CASE WHEN answers.question_value <> model.question_value THEN true ELSE false END AS is_changed
FROM (
SELECT case_part_question.case_part_id, case_part_question.case_part_question_id, treatment_day_question.question_value
FROM treatment_day_question
LEFT JOIN case_part_question ON (treatment_day_question.case_part_question_id = case_part_question.case_part_question_id)
LEFT JOIN case_part ON (case_part_question.case_part_id = case_part.case_part_id)
WHERE treatment_day_id = 145 AND case_part.active AND case_part.is_show_in_settings
ORDER BY case_part_id, case_part_question.case_part_question_id
) model
LEFT JOIN (
SELECT * FROM (
VALUES (1, 99, 0),(1, 168, 1),(1, 202, 0),(2, 6, 22),(2, 10, 1),(2, 11, 0)
) as a (case_part_id, case_part_question_id, question_value)
) answers
ON (model.case_part_id = answers.case_part_id AND model.case_part_question_id = answers.case_part_question_id)
;

Удаление дубликатов

DELETE FROM table_name
 WHERE table_name_id IN (
     SELECT
         table_name_id
     FROM (
         SELECT
             table_name_id,
             ROW_NUMBER() OVER w AS rnum
         FROM table_name
         WINDOW w AS (
             PARTITION BY field1, field2
             ORDER BY table_name_id
         )
     ) t
 WHERE t.rnum > 1);


Проверки JS

на undefined

if (typeof user == 'undefined') {}

пустой объект

if (Object.keys(obj).length == 0) {
    console.log('пуст');
}

Свойство объекта

let user = { name: "John", age: 30 };

alert( "age" in user ); // true, user.age существует
alert( "blabla" in user ); // false, user.blabla не существует

Обновить данные в таблице из JSON

В запросе приходит строка с JSON следующего вида:

[{"client_diagnoses_id":"11","treatment_id":97},{"client_diagnoses_id":"10","treatment_id":92},{"client_diagnoses_id":"12","treatment_id":96}]

Нужно изменить значение поля treatment_id. Преобразуем JSON в пару строк вида «11,10,12» и «97,92,96» следующим образом:

if (isset($_REQUEST['checked_diag'])) $diag_array = json_decode($_REQUEST['checked_diag'], true);
else exit();
$diag_count = count($diag_array);
$client_diagnoses_id = '';
$treatment_id = '';

if ($diag_count > 0){
for($counter=0; $counter<$diag_count; $counter++){
$client_diagnoses_id.= $diag_array[$counter]['client_diagnoses_id'].',';
$treatment_id .= $diag_array[$counter]['treatment_id'].',';
}
$client_diagnoses_id = substr($client_diagnoses_id,0,-1);
$treatment_id = substr($treatment_id,0,-1);

Потом вставляем

UPDATE client_diagnoses SET treatment_id = new_treatment.treatment_id
  FROM (
    SELECT unnest(array[$client_diagnoses_id]) AS client_diagnoses_id, unnest(array[$treatment_id]) AS treatment_id
    ) AS new_treatment
  WHERE client_diagnoses.client_diagnoses_id = new_treatment.client_diagnoses_id;

Страницы:123